long int computations

Jussi Piitulainen jpiitula at ling.helsinki.fi
Mon May 3 12:23:40 EDT 2010


Peter Otten writes:
> Victor Eijkhout wrote:
> 
> > I have two long ints, both too long to convert to float, but their
> > ratio is something reasonable. How can I compute that? The obvious
> > "(1.*x)/y" does not work.
> 
> >>> import fractions
> >>> x = 12345 * 10**1000
> >>> y = 765 * 10**1000
> >>> float(x)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> OverflowError: long int too large to convert to float
> >>> fractions.Fraction(x, y)
> Fraction(823, 51)
> >>> float(_)
> 16.137254901960784

Logarithms agree to 13 decimal digits or so:

>>> math.exp(math.log(x) - math.log(y))
16.13725490196353



More information about the Python-list mailing list