Peter Otten <__pete...@web.de> wrote: > 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 >
Another way of doing this is simply to scale the values down until they do fit in a float: >>> x = 12345 * 10**1000 >>> y = 765 * 10**1000 >>> maxfloat = 1e308 # or whatever it actually is... >>> d = max(max(abs(x),abs(y))//long(maxfloat), 1) >>> float(x//d)/float(y//d) 16.137254901960784 I think that should work for any cases where the ratio fits in a float (if it doesn't then you might get a ZeroDivisionError or inf or -inf). -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list