By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,060 Members | 1,878 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,060 IT Pros & Developers. It's quick & easy.

Error: invalid operands of types `double' and `double' to binary `operator%'

P: 1
I am trying to reduce fractions.
If I replace double by int,there is no error,and it will run answers. But the wrong one.....
I don't know if I use the reducing fractions' rule wrongly.
Can someone help me pleaseQQ
Expand|Select|Wrap|Line Numbers
  1. RationalNumber::RationalNumber(double a,double b)
  2. {
  3.     if(b>0)//prvent deno<0
  4.     {
  5.         double r;
  6.         if(a<0)//if nume<0
  7.         {
  8.             a=-a;
  9.             r=(a%b);
  10.             while(r!=0)
  11.             {
  12.                 a=b;
  13.                 b=r;
  14.                 r=a%b;
  15.             }
  16.             this->nume=-a;
  17.             this->deno=b;
  18.             cout<<"test2"<<nume<<"/"<<deno<<endl;
  19.         }
  20.         else
  21.         {
  22.             r=a%b;
  23.             while(r!=0)
  24.             {
  25.                 a=b;
  26.                 b=r;
  27.                 r=a%b;
  28.             }
  29.  
  30.             this->nume=a;
  31.             this->deno=b;
  32.             cout<<"test2"<<nume<<"/"<<deno<<endl;
  33.         }
  34.     }
  35.     else
  36.     {
  37.         cout<<"It's a invalid fraction."<<endl;
  38.         cout<<"The fraction will be set to 0/1"<<endl;
  39.         this->nume=0;
  40.         this->deno=1;
  41.         cout<<"test2"<<nume<<"/"<<deno<<endl;
  42.     }
  43. }
  44.  
  45.  
May 15 '15 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
The modulus operator % applies only to integers.

In set theory, the modulo 5 set contains only 0,1,2,3,4. So if you have an element of another set, say the base 10 integer number system, like 17, you convert to modulo five by 17 % 5 which produces 2. That 2 is the element value in the modulo 5 set.

Floating point, on the other hand, has no conversion to modulo 5 because the values of floating point are not defined. That is, 123.456..... has decimal places that go on forever. It can't be pinned down to a fixed value. Hence, no modulus conversion.
May 15 '15 #2

Expert 100+
P: 2,398
From a mathematics point of view, a rational number is defined as the ratio of two integers. I don't know what you get from the ratio of two real numbers.
May 16 '15 #3

P: 9
it should also be noted that those =-a statements could potentially, though not likely, cause trouble. =(-a) would probably better, or at the very least some spacing.
Jun 16 '15 #4

Post your reply

Sign in to post your reply or Sign up for a free account.