what's the precision of fractions.Fraction?

Terry Reedy tjreedy at udel.edu
Thu Nov 18 16:31:40 EST 2010


On 11/18/2010 11:27 AM, Daniel Fetchinson wrote:
> I do a recursive evaluation of an expression involving fractions and
> unsurprisingly the numerator and denominator grows pretty quickly.
> After 10-20 iterations the number of digits in the numerator and
> denominator (as integers) reaches 80-100. And I'm wondering until what
> point I can trust the result since I'm using fractions.Fraction for
> the whole procedure. Are Fraction's infinite precision?

'Unbounded' (except by memory) would be be more accurate.

> Or will I get
> some sort of an exception if python is not able to represent the
> numerator and/or denominator as integers?

I believe you could eventually get MemoryError. But you might get tired 
of waiting first. I tried

i=0
n=2
while True:
     print(i)
     n *= n
     i += 1

The 30th doubling (I believe to a billion bit or 128 megabyte number, on 
a gigabyte machine) took a few minutes and I killed the program after 
that as it tried to page everything else out to disk.


-- 
Terry Jan Reedy




More information about the Python-list mailing list