0

Im not being allowed to use mod. I think it's not working because I'm using doubles; is there a way around this? --- the commented areas arnt working

void displayResults(double num1, char oper, double num2)
{
     switch(oper)
     {
     case '+' :
     cout << num1 << "+" << num2 << "=" << (num1+num2) << endl;
     break;

     case '-' :
     cout << num1 << "-" << num2 << "=" << (num1-num2) << endl;
     break;

     case '*' :
     cout << num1 << "*" << num2 << "=" << (num1*num2) << endl;
     break; 

     case '/' :
          if ( num1==0 || num2==0)
          cout <<"A number divided by 0 or divided into 0 is always 0"<< endl;
          else
          cout << num1 << "/" << num2 << "=" << (num1/num2) /*+ (num1%num2) */ << endl;
          break; 
    // case '%' :
    // cout << num1 << "%" << num2 << "=" << (num1%num2);
    //break;
     }

}
matttm
  • 124
  • 1
  • 2
  • 15
  • 1
    You're not allowed? *How* is it not working? Compiler errors? Run-time Exceptions? Output not correct? – crashmstr Oct 21 '13 at 19:32

5 Answers5

5

Use std::fmod. It has an overload for doubles:

#include <cmath>

std::fmod(num1, num2);
David G
  • 94,763
  • 41
  • 167
  • 253
  • I don't really get it. Mathematically, for example, what's the remainder of 2.6/1.5? – khajvah Oct 21 '13 at 19:34
  • 1
    @khajvah I may be Batman, but I'm no mathematician. :-) – David G Oct 21 '13 at 19:41
  • @khajvah: If we want the remainder of a/b, where b is positive, write a = q * b + r. q has to be an integer, and we need 0 <= r < b. r is the remainder. I think of b is negative, then in math we use a positive r, but in programming a negative r is often used. – Ken Wayne VanderLinde Oct 21 '13 at 19:45
  • @KenWayneVanderLinde According to `cppreference.com`, absolute value of `a-q*b` has to be smaller than absolute value of `b`, so when `b` is negative, we pick negative `q`. As a result, remainder is the same in both cases. – khajvah Oct 21 '13 at 20:09
  • @khajvah: Ah yes, fmod gives the same sign as `a`. But this differs from it mathematical usage, so it's quite confusing sometimes! – Ken Wayne VanderLinde Oct 21 '13 at 20:14
1

The modulus operator % is an integral function.

You need to use fmod for floating-point.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
1

% is only allowed for integral types or unscoped enums, you can use std::fmod for double:

#include <cmath>
#include <iostream>

int main() {
    double num1 = 5.5;
    double num2 = 3.0;
    double z = std::fmod(num1,num2);

    std::cout << z << std::endl ;
    return 0;
}
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
0

Of course, you could overload the '%' operator to perform the modulus operation on any type you like. There is no build-in support in the language for doubles, however.

ThunderGr
  • 2,297
  • 29
  • 20
0

i think you can rather replace your doubles with 'long int' , that will help you overcome the error and this will also work with all integer based operations.

i made this change in one of my codes and got it right, with a warning though. But it did work just fine, so try that, it should work pretty well.

sid
  • 1