Reducing rationals

Can someone please walk me through the logic in this reducing rationals syntax. My teacher posted it for us to use in a program but I just don't understand the methods used to reduce.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void Rational::reduce()
{
    bool negative = (numerator < 0) != (denominator < 0);
    numerator = abs(numerator);
    denominator = abs(denominator);

    int factor = 2;
    while (factor <= min(numerator, denominator))
    {
       if (numerator % factor == 0 && denominator % factor == 0)
       {
           numerator = numerator / factor;
           denominator = denominator / factor;
           continue;
       }
       factor ++;
    }
    if (negative) 
       numerator = - numerator;
}


bool negative = (numerator < 0) != (denominator < 0);
Particularly, this is bothering me. What is this saying. You have a boolean
variable negative but it's equal to something not T or F? How is this possible?
bool negative = (numerator < 0) != (denominator < 0);
Particularly, this is bothering me. What is this saying. You have a boolean
variable negative but it's equal to something not T or F? How is this possible?

0 is false anything else is true. numerator < 0 returns 1 if it is negative and 0 if it positive. The same with the denominator so then this says
if They are equal ( both negative or both positive ) negative is false if they are different ( one negative other positive ) then it is a negative number.

Then line 4-5 make the numerator and denominator both positive numbers.

Then lines 7-17 pretty much say this:

Starting factor is 2 and loop until the factor is equal to the lowest value ( numerator or denominator ). Then on line 10 it checks that the factor is really a factor ( divides evenly into numerator and denominator ). If it is a factor then divide by the numerator and denominator by it.

Now on line 18 if the number started as negative make the numerator negative ( resulting in a negative rational number again ).

*fixed code tag
Last edited on
Particularly, this is bothering me. What is this saying. You have a boolean variable negative but it's equal to something not T or F? How is this possible?

Actually, it is equal to true or force. It might make it easier if I break it down for your:
1
2
3
4
5
6
7
8
9
10
11
12
bool negative = (numerator < 0) != (denominator < 0);
// Example: numerator is negative and denominator is positive
negative = (true) != (false);
negative = true;

// Example: numerator is positive and denominator is positive
negative = (false) != (false);
negative = false;

// Example: numerator is negative and denominator is negative
negative = (true) != (true);
negative = false;


As for how the program works, it sees if the numerator and the denominator has a common factor, and if they do, it divides them both by it (as it should be the same value overall). This is done by using the modulo operator to see if the division has a remainder.

EDIT:
Ninja'd by @giblit
Last edited on
I love you guys so much. Bookmarking this to study. I appreciate it a lot. <3

It's funny because understanding line 3 kind of reminded me what we were doing in the first few weeks of discrete math this semester. It's funny how these things tie together so closely.
Topic archived. No new replies allowed.