reduce fraction

closed account (zwA4jE8b)
I am working on a rational number class and am a little stumped on reducing the fractions.

Can anyone give me an algorithm? Not necessarily the code.

I have two variables, double num (numerator) and double den (denominator)

If they were integers I would just use % to find the highest divisor.

Thank you,
Mike
Last edited on
Go check out the Sources section on this forum and look at the second source.
http://www.cplusplus.com/src/
closed account (zwA4jE8b)
thank you, and that is good reading.

But the problem is that I am using doubles, so using modulus is out of the question.
That was my original idea though.

I guess I could convert the doubles to integers by multiplying them by 10^x depending on how many decimal places but I would like to not have to do that.
closed account (1yR4jE8b)
I guess I could convert the doubles to integers by multiplying them by 10^x depending on how many decimal places but I would like to not have to do that.


I suggest you do this, in fact, it's what I told my students to do when I made them implement a Fraction class in Ruby.

Converting doubles to fractions are tricky business, mostly because of the way that doubles are represented in binary. This is a long one, but it's a really good read.

http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html
Topic archived. No new replies allowed.