Advanced Math/Finding the nearest fraction
Expert: Ahmed Salami - 7/13/2004
QuestionI need an algorithm which, given a decimal value, will return the nearest fraction (1/9, 1/8, 1/6, 1/5, etc.)
I tried the following: Divide 1 by the value, then round the result to get the denominator. This works fine as long as the numerator is 1, but I also need to be able to tell if, for instance, 0.722 is closer to 2/3 or 3/4.
Any suggestions?
Thanks,
-- Bob
AnswerHi Bob,
Every decimal value can be converted to a fraction except it is an irrational number. For instance 0.722 is 361/500, so the decimal value is itself a fraction and i don't think one can talk of a nearest fraction except with some specifications.
Your method of finding reciprocals is obviously the closest you could do, i guess.
Get back to me for this or any other thing.
Hope i have helped.
Regards.