Overflow error

Satchidanand Haridas sharidas at zeomega.com
Wed Jul 28 03:44:57 EDT 2004


janeaustine50 at hotmail.com <http://mail.python.org/mailman/listinfo/python-list> (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?
/
While float may not be able to handle such large numbers, the 'long' data-type is capable of unlimited precision limited only by your system memory. 

You could cast the above floats to long and then do the needed calculation. But note that long floors a floating number. So:

long(e) will return 2.

So you will have to consider the fractional part while multiplying to avoid rounding-off errors. Something like this:

>>> intE = long(e)
>>> fracE = e - intE
>>> result = long(e**709) * intE + long(fracE * e**709)

Hope this was of some help. 

Regards,
Satchit

----
Satchidanand Haridas (sharidas at zeomega dot com)

ZeOmega (www.zeomega.com)
Open  Minds' Open Solutions

#20,Rajalakshmi Plaza,
South End Road,
Basavanagudi,
Bangalore-560 004, India




More information about the Python-list mailing list