From: Victor Eijkhout on 3 May 2010 11:17 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. Victor. -- Victor Eijkhout -- eijkhout at tacc utexas edu
From: Peter Otten on 3 May 2010 11:30 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
From: Jerry Hill on 3 May 2010 11:41 On Mon, May 3, 2010 at 11:17 AM, Victor Eijkhout <see(a)sig.for.address> 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 didn't say what version of python you were using, but this seems to work for me in 2.6.4: >>> long1 = 10**1000 >>> long2 = long1 * 2 >>> float(long1) Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> float(long1) OverflowError: long int too large to convert to float >>> long1/long2 0L >>> from __future__ import division >>> long1/long2 0.5 The "from __future__ import division" line gets python to return a float as the result of dividing two integers (or longs), instead of returning an integer. If I recall correctly, this has been available for quite a few python versions (since 2.2 maybe?), and has become the default in python 3. If you need to do integer division, you would use the // operator: >>> long1 // long2 0L Hope that helps. -- Jerry
From: Dave Angel on 3 May 2010 12:10 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. > > Victor. > > You don't make clear what you mean by "too long to convert to float." Do you mean can't convert exactly, or that they're more than 400 digits or whatever the exponent limit of float is (I'm too lazy to look it up right now) ? Assuming the latter, you could divide each by the same constant, then use your formula above to make a float version of the ratio. How to pick the constant? How about the smaller of your numbers, divided by 1e18 ? You'd have to build that number using longs as well. Alternatively, you could build your own floating point library, using longs as the mantissa. DaveA
From: Peter Pearson on 3 May 2010 12:16 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 "<stdin>", line 1, in <module> > 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. (I blush to confess that my Python has no fractions module, and it's too early in the morning to risk trying to update anything.) -- To email me, substitute nowhere->spamcop, invalid->net.
|
Next
|
Last
Pages: 1 2 3 Prev: strange interaction between open and cwd Next: Django as exemplary design |