what's the precision of fractions.Fraction?

Peter Otten __peter__ at web.de
Sat Nov 20 13:59:27 CET 2010

Hrvoje Niksic wrote:

> Mark Dickinson <dickinsm at gmail.com> writes:
>> On Nov 19, 3:29 pm, RJB <rbott... at csusb.edu> wrote:
>>> Does Fractions remove common factors the way it should?
>>> If it does and you want to find the closest fraction with a smaller
>>> denominator i think tou'll need some number theory and continued
>>> fractions.
>> Or perhaps just use the existing Fraction.limit_denominator method
>> (which does indeed use some number theory and continued fractions):
>>>>> from fractions import Fraction
>>>>> from math import pi
>>>>> Fraction.from_float(pi).limit_denominator(1000)
>> Fraction(355, 113)
> People, this is exactly what http://xkcd.com/353/ is about.

I can't help thinking of http://xkcd.com/378/ with the punch line changed to

"'course there's a python library function to do that"

It's all butterflies from here on...


More information about the Python-list mailing list