LOGIC????

I get the logic behind all of these. But I dont understand "bool Rational::isEqualTo(const Rational a) const{" function. How does that check if two rational number are equal. And this function "bool Rational::isGreaterThan(const Rational a) const {" Doesnt make any sence to me. Can someone expalin to me how the logic behind it is???
thnxk

//constructs a rational number with the given
//numerator part; denominator will be initialized to 1

Rational::Rational (int numerator ){
numer=numerator;
denom = 1;

//default constructor

Rational::Rational() {
numer=0;
denom=1;
}

//constructs a rational number with the given
//denominator and nominator parts

Rational::Rational (int numerator, int denominator ){
numer=numerator;
denom=denominator;
fixSigns();
reduce();

}

//returns true if the two rational numbers
//are equal, false otherwise

bool Rational::isEqualTo(const Rational a) const{
return (numer==a.numer && denom==a.denom);
}

//returns true if one rational number is greater than the other

bool Rational::isGreaterThan(const Rational a) const {
return ((numer*a.denom - denom*a.numer)>0);
}
Actually, isEqualTo() fails for equal fractions if the denominators differ... I'll come back to this in a second.

The isGreaterThan() function uses a technique called cross multiplication. Read more here:
http://www.mathsisfun.com/algebra/cross-multiply.html

Using the cross product on this and a gives the following possibilities:

first cross product = second cross product: first fraction = second fraction
first cross product < second cross product: first fraction < second fraction
first cross product > second cross product: first fraction > second fraction

Using the first identity you can fix the problem with the isEqualTo() function.

Hope this helps.
Last edited on
In mathematics, a rational number is a number which can be expressed as a ratio of two integers.
Q={m/n, m "belong to" Z, n "belong to" Z, n != 0}

Z denotes the set of integers.

a "belong to" Q, b "belong to" Q
=>a,b can be expressed as to this:
a = m1/n1,b = m2/n2
=> "Equal"
a = b <=> m1= m2 and n1 = n2

=>"GreaterThan"

a - b > 0 <=> m1/n1 - m2/n2 > 0

=> (m1 * n2 - n1* m2 ) / ( n1 * n2 ) > 0

1. if ( n1 * n2 ) > 0 <=> n1 , n2 's sign is same!
=> a - b > 0 <=> ( n1 * n2 ) > 0

2. if ( n1 * n2 ) < 0 <=> n1 , n2 's sign is not same!
=> a - b > 0 <=> ( n1 * n2 ) < 0
nice copy and paste from Wiki Simo110, but you should reference your sources.
Last edited on
Wiki?
O_O

Topic archived. No new replies allowed.