426,060 Members | 1,878 Online 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 RationalNumber::RationalNumber(double a,double b) {     if(b>0)//prvent deno<0     {         double r;         if(a<0)//if nume<0         {             a=-a;             r=(a%b);             while(r!=0)             {                 a=b;                 b=r;                 r=a%b;             }             this->nume=-a;             this->deno=b;             cout<<"test2"<nume=a;             this->deno=b;             cout<<"test2"<nume=0;         this->deno=1;         cout<<"test2"<
3 Replies

 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 