operators overload?

Hello everyone,

I am trying to add a object of class with a double value
but the result is wrong becasue, double value is stored as an integer

I am posting some examples and hopefully someone can point me in the right direction.

1
2
3
2/3 + 2.75 = 8/3  // because the 2.75 is converted in 2/1

2/3 + 2.75  =  2/3 + 11/4  = 3 5/12 // this is what the step process should be and output 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
CFraction fr, fr1, fr2; // object of a class
double x;

fr2 = fr + x; //here when i run this x is as an integer
                   // and the result is wrong
return 0;
}
// an example of how I am trying to do this addition
CFraction CFraction :: operator + (double p)const
{
	CFraction plus(p);
	
	return *this + plus;
}




I am not sure if I need a function tha converts a decimal into a fraction or I can do that on the operator function shown above?

thanks in advance
Last edited on
I think the idea of converting a float or double to a rational number is a bit ambitious.

For humans 2.75 may sound like 11/12 but to computer the accuracy of floats does not provide this step.
For example what is the difference between 2.75 and 2.75000000000000000000000000000000000000001? For human there is but for computer it's the same. Anyway you can always provide an approximation method (in which the above latter number would equals 2.75). You must still find a smart way to approximate rationals!

To do a conversion to rational you can implemented in various ways:
1) via a constructor of the form CFraction(double)

2) by defining a conversion method of CFraction to double

3)even defining a template.

I think it's easier to convert your number to double than the other way around when involves actions with doubles but I think you don't want that. Good luck!
thanks for the fast reply eypros,

I wanted to use the 1 idea CFraction(double)

but I get stuck at what to write inside, how to implement the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class CFraction
{
  private:
int num, den;

public:

CFraction( int = 0, int = 1);
CFraction(float)
// and so on
};

int main()
{
    CFraction fr, fr1, fr2;

fr = fr1 + x;

cout << fr1 <<" +" << x << " = " << fr;

// or this method
fr2 = x;
fr = fr1 + fr2;

cout << fr1 <<" +" << fr2<< " = " << fr;


return 0;

}

Cfraction::Cfraction()
{
    // this is the part on I just frezze

}

I forgot to mention that you can implement also operator overloading (and maybe it's better to use this one).
This means you can overload for example operator+ and/or operator- etc.

Using the constructor method means you implicitly convert a double to a CFraction. That's not always desirable. Overloaded operators means you don't have to implicitly do it.

The point you stuck is the difficult one: how do you convert a floating point num to rational?
You have to figure it out. Decisions have to be made about whether 0.83 equals 5/6, 0.999697428 equals 3304/3305 (it does but how do you figure it out?) etc

Constructor should be something like this (that's just an approach):
1
2
3
4
5
6
Cfraction::Cfraction(double d)
{ 
create a Cfraction
convert double to Cfraction
assign num, den
return Cfraction}


Topic archived. No new replies allowed.