473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
3 5112
weaknessforcats
9,208 Expert Mod 8TB
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
donbock
2,426 Expert 2GB
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
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

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

Similar topics

12
by: Sydex | last post by:
When I compile code I get error C2664: 'Integration::qgaus' : cannot convert parameter 1 from 'double (double)' to 'double (__cdecl *)(double)' in this part : double Integration::quad2d(double...
0
by: Frank King | last post by:
Hi, I am using CArray and quick sort funciton to sort an array of double type of data points. I found an article in MSDN HOWTO: Quick Sorting Using MFC CArray-Derived Classes ID: Q216858 ...
4
by: muthu | last post by:
In the following code it gives the error "error: invalid operands to binary &" Why it is happening #include <signal.h> #include <errno.h> #define SIGBAD(signo) ((signo) <= 0 || (signo) >=...
2
by: xelloss | last post by:
#include<cstdlib> #include<iostream> #include<iomanip> #include<vector> #include<fstream> using namespace std; double sum(vector<double> x) { double total = 0.0;
2
by: clintonb | last post by:
Victor said: The double value that I'm trying to convert to GCSMoney (which is implemented as cents) was produced by multiplying a dollar amount by an interest rate to get interest. double...
2
by: Genro | last post by:
#include<stdio.h> #include<TX/graphics.h> #include<time.h> // I need help! struct Krug{ double _x; double _y; double _skox; double _skoy; double...
2
by: Tyler Palmer | last post by:
I am having a problem with my program. I cant figure out a solution for the compiler error im getting. In function `double endingConversion(double)': invalid operands of types `double...
2
by: SunDevil1171 | last post by:
I making a simple single number scrambler/encryptor and as I tried to build the console application the following error occurred: error: invalid operands of types 'void' and 'int' to binary...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.