long int computations

Peter Otten __peter__ at web.de
Mon May 3 11:30:03 EDT 2010


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

Peter



More information about the Python-list mailing list