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
Convert A Number To Its Closest Fraction
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>.