Local Variables
You might have noticed that the result of adding 1/4 to 1/2 was displayed as 6/8, not as 3/4, which you might have preferred (or even expected!). That’s because your addition routine just does the math and no more; it doesn’t worry about reducing the result. So, to continue our exercise of adding new methods to work with fractions, let’s make a new reduce method to reduce a fraction to its simplest terms.
Reaching back to your high school math again, you can reduce a fraction by finding the largest number that evenly divides both the numerator and the denominator of your fraction and then dividing them by that number. Technically, you want to find the greatest common divisor (gcd) of the numerator and denominator. You already ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access