DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Convert A Number To Its Closest Fraction

09.18.2005
| 9206 views |
  • submit to reddit
        Sometimes, I need to find a ratio approximation of a number.
Like 640 / 480 (vga) or similar number. I learn about farey
series a few years ago (1994). 
The <a href=http://en.wikipedia.org/wiki/Farey_sequence>idea</a> is actually quite simple.
>>> farey(math.pi,100)
(22, 7)
Get the implementation <a href=http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52317>here</a>.