You are here:

Advanced Math/Finding the nearest fraction

Advertisement


Question
I 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

Answer
Hi 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.

Advanced Math

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ahmed Salami

Expertise

I can provide good answers to questions dealing in almost all of mathematics especially from A`Level downwards. I can as well help a good deal in Physics with most emphasis directed towards mechanics.

Experience

An engineering graduate. I have been doing maths and physics all my life.

©2012 About.com, a part of The New York Times Company. All rights reserved.