Overflow error

Dan Bishop danb_83 at yahoo.com
Thu Jul 29 09:37:13 CEST 2004


Michael Hudson <mwh at python.net> wrote in message news:<m3k6wpk2i9.fsf at pc150.maths.bris.ac.uk>...
> janeaustine50 at hotmail.com (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**8)):
...     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))
223399476616171103125364445811681000656812286337946419939922579763369439173505508238045208936075928608008858947959672204126540307964255760331629484074081710600724815623037686564199430826371986947985157927836355814874856465984698389900107606439843841800268119591413945009951691796042715693932113514608158683164L

However, I don't recommend this, because it's *very* slow.



More information about the Python-list mailing list