Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Overflow error

Reply
Thread Tools

Overflow error

 
 
Jane Austine
Guest
Posts: n/a
 
      07-27-2004
>>> from math import e
>>> e**709

8.218407461554662e+307
>>> e**710


Traceback (most recent call last):
File "<pyshell#15>", line 1, in -toplevel-
e**710
OverflowError: (34, 'Result too large')

What should I do to calculate e**710?

I'm using Python 2.3.4 on WinXP.
 
Reply With Quote
 
 
 
 
Michael Hudson
Guest
Posts: n/a
 
      07-27-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Jane Austine) writes:

> >>> from math import e
> >>> e**709

> 8.218407461554662e+307
> >>> e**710

>
> Traceback (most recent call last):
> File "<pyshell#15>", line 1, in -toplevel-
> e**710
> OverflowError: (34, 'Result too large')
>
> What should I do to calculate e**710?


Well, it's too big for your platform's C double, so you need a
different representation. I don't know if there are big float
packages out there that handle such things (likely, though) or if
there are Python interfaces to the same (less likely). Or you could
store the logarithms of the numbers you are interested in. Why do you
need such huge nubmers?

Cheers,
mwh

--
<exarkun> today's lesson
<exarkun> don't strace X in an xterm
-- from Twisted.Quotes
 
Reply With Quote
 
 
 
 
Scott David Daniels
Guest
Posts: n/a
 
      07-28-2004
Michael Hudson wrote:

> (E-Mail Removed) (Jane Austine) writes:
>
>
>>>>>from math import e
>>>>>e**709

>>
>>8.218407461554662e+307
>>
>>>>>e**710

>>
>>Traceback (most recent call last):
>> File "<pyshell#15>", line 1, in -toplevel-
>> e**710
>>OverflowError: (34, 'Result too large')
>>
>>What should I do to calculate e**710?

>
>
> Well, it's too big for your platform's C double, so you need a
> different representation. I don't know if there are big float
> packages out there that handle such things (likely, though) or if
> there are Python interfaces to the same (less likely). Or you could
> store the logarithms of the numbers you are interested in. Why do you
> need such huge nubmers?
>
> Cheers,
> mwh
>

Using a little bit of magic:

First get a good approximation of e:
Using my bits package, I can do:

import bits, math
scaling = bits.lsb(math.e)
characteristic = bits.extract(math.e, scaling, 10)
# This has the same effect as:
# scaling, characteristic = -51, 6121026514868073L
# Now e = characteristic * 2.**scaling
result_scaling = scaling * 710
result_characteristic = characteristic ** 710
intpart = result_characteristic >> -result_scaling
# and you'll have to grab as much fractpart as you want.

Similarly, for decimal, type in enough digits (for your taste) of e
from some reference book, and omit the decimal point.
Then track the exponent in base ten, and you can obtain similar results:

scaling, characteristic = -5, 271828
result_scaling = scaling * 710
result_characteristic = characteristic ** 710
intpart = result_characteristic // 10 ** -result_scaling


So, you needn't use floating point if you are willing to type in
constants. Both of these give a number which is 223. * 10 ** 50 to
three digits, and they differ in the fourth digit (4 or 3 if you round).
The binary-based version (the first above) produces:
2233994766....
While the decimal-based version produces:
2232928102....
This is certainly due to using so few decimal places for e in the
decimal version.
In Knuth's Art of Computer Programming (at least volume 3, which I
happen to have at hand) Appendix A, you can get 41 decimal digits for e,
or 45 octal digits if you prefer to work with binary. I believe
(without checking) that each of the volumes contains this appendix.
The big advantage of using decimal is (a) more readily available tables
of constants come in decimal than in binary, and (b) if you _do_ want
to print some of the fractpart, it is easier just to change the division
to include the extra digits, while for the binary versions you'll have
to multiply by 10**digits before the division.

--
-Scott David Daniels
(E-Mail Removed)
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      07-28-2004
(E-Mail Removed) (Jane Austine) writes:
> >>> from math import e
> >>> e**709

> 8.218407461554662e+307
> >>> e**710

>
> Traceback (most recent call last):
> File "<pyshell#15>", line 1, in -toplevel-
> e**710
> OverflowError: (34, 'Result too large')
>
> What should I do to calculate e**710?


Is this a homework problem?

Aw heck, you can do something like this:

from math import *
a,b = divmod(710 * log10(e), 1.0) # int and frac parts of log10(e**710)
print '%f * 10**%d'%(10.** b, a)

This prints: 2.233995 * 10**308
 
Reply With Quote
 
Dan Bishop
Guest
Posts: n/a
 
      07-29-2004
Michael Hudson <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> (E-Mail Removed) (Jane Austine) writes:
>
> > >>> from math import e
> > >>> e**709

> 8.218407461554662e+307
> > >>> e**710

> >
> > Traceback (most recent call last):
> > File "<pyshell#15>", line 1, in -toplevel-
> > e**710
> > OverflowError: (34, 'Result too large')
> >
> > What should I do to calculate e**710?

>
> Well, it's too big for your platform's C double, so you need a
> different representation. I don't know if there are big float
> packages out there that handle such things (likely, though) or if
> there are Python interfaces to the same (less likely).


You can also do it with rationals:

>>> def unboundedRange(start=0):

.... n = start
.... while True:
.... yield n
.... n += 1
....
>>> def exp(x, tolerance=rational(1, 10**):

.... total = term = 1
.... for n in unboundedRange(1):
.... term *= rational(x, n)
.... total += term
.... if abs(term) < tolerance:
.... break
.... return total
....
>>> float(exp(1))

2.7182818282861687
>>> long(exp(710))

22339947661617110312536444581168100065681228633794 64199399225797633694391735055082380452089360759286 08008858947959672204126540307964255760331629484074 08171060072481562303768656419943082637198694798515 79278363558148748564659846983899001076064398438418 00268119591413945009951691796042715693932113514608 158683164L

However, I don't recommend this, because it's *very* slow.
 
Reply With Quote
 
Jane Austine
Guest
Posts: n/a
 
      07-31-2004
(E-Mail Removed) (Dan Bishop) wrote in message news:<(E-Mail Removed). com>...
> Michael Hudson <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> > (E-Mail Removed) (Jane Austine) writes:
> >
> > > >>> from math import e
> > > >>> e**709

> 8.218407461554662e+307
> > > >>> e**710
> > >
> > > Traceback (most recent call last):
> > > File "<pyshell#15>", line 1, in -toplevel-
> > > e**710
> > > OverflowError: (34, 'Result too large')
> > >
> > > What should I do to calculate e**710?

> >
> > Well, it's too big for your platform's C double, so you need a
> > different representation. I don't know if there are big float
> > packages out there that handle such things (likely, though) or if
> > there are Python interfaces to the same (less likely).

>
> You can also do it with rationals:
>
> >>> def unboundedRange(start=0):

> ... n = start
> ... while True:
> ... yield n
> ... n += 1
> ...
> >>> def exp(x, tolerance=rational(1, 10**):

> ... total = term = 1
> ... for n in unboundedRange(1):
> ... term *= rational(x, n)
> ... total += term
> ... if abs(term) < tolerance:
> ... break
> ... return total
> ...
> >>> float(exp(1))

> 2.7182818282861687
> >>> long(exp(710))

> 22339947661617110312536444581168100065681228633794 64199399225797633694391735055082380452089360759286 08008858947959672204126540307964255760331629484074 08171060072481562303768656419943082637198694798515 79278363558148748564659846983899001076064398438418 00268119591413945009951691796042715693932113514608 158683164L
>
> However, I don't recommend this, because it's *very* slow.


Interesting. Where do I get the "rational" module?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I found a bad error: queue overflow, no 281 lenth = 2b8c, can you help me? greatwall Java 3 05-27-2006 07:04 PM
Stack overflow error on win 2k3 server.. please help paragdere@gmail.com ASP .Net 0 11-23-2005 10:07 AM
Stack overflow error on win 2003 server paragdere@gmail.com ASP .Net 0 11-23-2005 06:55 AM
"Overflow" error message.... Dewayne Smith Computer Information 0 12-31-2003 02:13 AM
floating point error: overflow? C. Sengstock C Programming 3 09-26-2003 06:29 AM



Advertisments