I'm trying to see how I can set two fractions equal to eachother. With that, I have created a class called Fraction() that should be able to take two parameters in it such as Fraction One(5, 5) and Fraction Two(1, 1) to see if they equal to eachother, or if one of them is less or more.
Currently I have this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class Fraction{
public:
Fraction(){}
Fraction(int x, int y){
first_num = x;
second_num = y;
total = first_num/second_num;
returnTotal(total);
}
double returnTotal(double n){
cout << n << endl;
return n;
}
private:
int first_num;
int second_num;
double total;
};
Now, the only thing I can do is get the Fraction One(5, 5) and Fraction Two(1, 1), But I don't have any way of comparing these to eachohter. It will just print out the total of both of these. And if I try to continue in main with comparison it won't let me.
Side-note: your example main() declares two variables, One and Two but the condition has two unknown names f1 and f2. You should compare One and Two.
Similar example:
1 2 3 4 5 6 7 8 9 10
#include <iostream>
int main(){
int One {7};
int Two {42};
if ( One == Two ) {
std::cout << "They are equal\n";
}
return 0;
}
When is one fraction equal to an another?
Is it when One.total == Two.total ?
One could think so. However, your Fraction::total is a double. A floating point value. Comparing them is strange and weird. Definitely not intuitive.
I have to ask why you create fractions from doubles? Would you create a fraction
2.718 / 3.14
Perhaps it would be enough to have "whole" fractions, like 4 / 3 or 16 / 12? Those you can compare with integer math, although you do need to do a bit more legwork.
@keskiverto I cleaned up the code with ur suggestions, the f2 and f1 is me misspelling. Sorry!
Would you be so kindly to assist me in the right way of comparing these?
I did some stuff with the fraction, would this be a good start on it? (I just made addition first, because that seemt to be the easiest way of grasping the concept). I'm a bit unsure on how to do this with comparing though.
if its a high precision fraction class, you can do all the common denominator juggling to see if 2 fractions are equal.
if its for practical usage, ...
if fabs(num1/(double)denom1 - num2/(double)denom2) < 0.000000001 //your favorite tolerance value here
they are equal.
something like that in the body of your operator == overload.
comparison operator overloads tend to get redundant. you may want to make a private function that returns something like (in this case) the difference from that equation, and then you can do this...
operator == as above, if (fabs(fx(a,b)) < tinynumber) then equal else not
operator < if not equal, then (if fx(a,b) < 0) its true
operator > if not equal, then (if fx(a,b) > 0) its true
and <= and >= etc... typically you need <, >, ==, <=, >=, and possibly !=
class Fraction{
public:
Fraction(){}
Fraction(int x, int y){
first_num = x;
second_num = y;
total = first_num/second_num; // Note: this is an integer division
returnTotal(total);
}
double returnTotal(double n){
cout << n << endl;
return n;
}
// Note:
booloperator==(const Fraction& x) const {
return (first_num == x.first_num) && (second_num == x.second_num);
}
private:
int first_num;
int second_num;
double total; // Note: this is not needed since it can be calculate any time
};
I should've removed that from the part there @lastchance. It was just a test. It wasn't supposed to be a mathematical way of adding with fraction there. Just a way of grasping the concept of operator overloading.
Furthermore, lets suppose that you notice an error in the logic. Since you essentially have the same logic in op< and op>, you have to fix both. That is tedious and error-prone. The oneliner version of op> above needs no maintenance; it is automatically correct, if op< is fixed.
This won't work if you expect 1/2 to == 2/4 etc.
that is why I suggested simply comparing the division... you could reduce the fractions to a common denominator and compare, but its a lot of work vs just 2 divisions and a comparison, both in code and internally. If you need absolute perfect precision, eg a banking program using fractions, then you should use the difficult method. If its just a fraction calculator for playing around, division is sufficient.