Hi, so I had to make a Fraction class for a programming class. It was supposed to convert the fractions to a decimal, reduce them, and use the overloaded ==, <, <=, >, >= operators to compare the fractions. I submitted it and my professor said the following - "If you return an int/int it will return an int: 1/2=0.
You need to return 1.0*numerator/denominator.
The easiest way to compare two fractions is to compare the toDecimal values." I don't really understand what she wants me to do. Here's all of my code.
Your code won't compile as it is because you never defined what variable "frac" was in your operator >
Also it seems you've edited your code because you've gained a partial understanding of what your teacher meant, but in case you weren't quite sure, what she's basically telling you is: integer arithmetic results in whole numbers while floating point and higher grade arithmetic results in more accurate numbers.
Ex. In integer arithmetic, 5/8 = 0. In floating point, 5/8 = 0.625.
And she's also saying that for an easier time of comparison, either use your own written toDecimal method that multiplies everything by 1.0 and therefore converts into float, or you can just straight up cast it into a float like: float(5/8)
Oops! I must've accidentally left that there. I was messing with it before to try and figure out how to fix the toDecimal thing. It used to compile. Let me put that back.
I understood integer arithmetic. I didn't understand why there was an issue in the first place? I defined everything as doubles to ensure there wasn't an issue.
How do I use the toDecimal method within the overloaded functions? I don't understand how I'm supposed to do that.
What do you mean? Your toDecimal function is a class function, part of your Fraction, so it can access any variable within the class scope. Since toDecimal returns a double, all you have to do is compare it to your class objects
1 2 3 4 5
Fraction frac1; // I assume you have it initialized to some value already
Fraction frac2; // same as before
if( frac1.toDecimal() < frac2.toDecimal() ) // since you already have an overloaded operator. it's just a class object calling on a class's member function
// etc.
You don't need to put it in the class because you already have it in the class. You have basically every overloaded operator you'd need for this level of arithmetic, and you already have your toString and toDecimal class functions.
Just look at line 175 in your code. Notice how you have a class object (frac) and have called a class function (toString). Same principle, but with if statements.
Okay. I thought she was saying my overloaded operator functions were wrong? As in I thought she wanted me to use the toDecimal function within the overloaded operator function to compare the fractions and couldn't figure out how I was supposed to do this. Like this (except this obviously very wrong.) -
I suspect you'll lose points for having numerator and denominator as doubles instead of integers. After all, Frac f(18.3457, 0.338473) probably shouldn't even be legal.
Line 17: if denom == 0, then denominator is uninitialized. I'd set it to denom in all cases and deal with the possibility of infinity as it comes up.
Line 62: So if f=2/3 then f++ is 3/4? That doesn't seem right.
Line 68: Similar comment as line 62.
Lines 85-90: I think the prof meant that you can replace this with return (toDecimal() < right.toDecimal());. Same with the other comparison operators.
The easiest way to compare two fractions is to compare the toDecimal values." I don't really understand what she wants me to do. Here's all of my code.
Sounds like the hardest way to me.
If you have two fractions a/b and c/d then,
if a/b == c/d then a * d - b * c == 0
if a/b >= c/d then a * d - b * c >= 0 etc etc
From memory a * d - b * c is called the discriminant which is constant for all the tests (whatever the name).