From: Jussi Piitulainen on 3 May 2010 12:23 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 "", line 1, in > 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 From: Peter Otten on 3 May 2010 12:39 Peter Pearson wrote: > On Mon, 03 May 2010 17:30:03 +0200, Peter Otten <__peter__(a)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 "", line 1, in >> OverflowError: long int too large to convert to float >>>>> fractions.Fraction(x, y) >> Fraction(823, 51) >>>>> float(_) >> 16.137254901960784 > > Does this still work if y = 765 * 10**1000 + 1 ? It looks > as if it might rely on x and y having a large common divisor. You mean something like this? >>> import fractions >>> x = 765 * 10**1000 >>> fractions.gcd(x, x+1) 1L>>> float(fractions.Fraction(x, x+1)) 1.0 But my D'oh!* moment really was seeing Jerry Hill doing it with >>> from __future__ import division >>> x/(x+1) 1.0 Peter (*) I'm having a lot of these lately. Looks like I need a new brain. From: Victor Eijkhout on 3 May 2010 16:49 Jerry Hill wrote: > >>> from __future__ import division > >>> long1/long2 > 0.5 Beautiful. Thanks so much guys. Victor. -- Victor Eijkhout -- eijkhout at tacc utexas edu From: Mensanator on 4 May 2010 12:50 On May 3, 10:17 am, s...(a)sig.for.address (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. You could try using the gmpy module. It supports arbitrary precision floats, so converting long to float is no problem. YY = 3**10684 float(YY) Traceback (most recent call last) File "pyshell#78>", line 1, in float(YY) OverflowError: Python int too large to convert to C double import gmpy gmpy.mpf(YY) mpf('3.6600365709...0197681e5097',16936) > > Victor. > > -- > Victor Eijkhout -- eijkhout at tacc utexas edu From: Victor Eijkhout on 4 May 2010 13:35 Mensanator wrote: > You could try using the gmpy module. It supports arbitrary precision > floats, so converting long to float is no problem. I fear I may actually have to go symbolic. I'm now having to use the 12th root of 2, and I would like the twelfth power of that to be exactly 2. Victor. -- Victor Eijkhout -- eijkhout at tacc utexas edu First  |  Prev  |  Next  |  Last Pages: 1 2 3 Prev: strange interaction between open and cwdNext: Django as exemplary design