473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Overflow error

>>> 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.
Jul 18 '05 #1
5 6535
ja***********@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). 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
Jul 18 '05 #2
Michael Hudson wrote:
ja***********@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). 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
Sc***********@Acm.Org
Jul 18 '05 #3
ja***********@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?


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
Jul 18 '05 #4
Michael Hudson <mw*@python.net> wrote in message news:<m3************@pc150.maths.bris.ac.uk>...
ja***********@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))

22339947661617110312536444581168100065681228633794 64199399225797633694391735055082380452089360759286 08008858947959672204126540307964255760331629484074 08171060072481562303768656419943082637198694798515 79278363558148748564659846983899001076064398438418 00268119591413945009951691796042715693932113514608 158683164L

However, I don't recommend this, because it's *very* slow.
Jul 18 '05 #5
da*****@yahoo.com (Dan Bishop) wrote in message news:<ad**************************@posting.google. com>...
Michael Hudson <mw*@python.net> wrote in message news:<m3************@pc150.maths.bris.ac.uk>...
ja***********@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))

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?
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: JKop | last post by:
You know how the saying goes that *unsigned* overflow is... well.. defined. That means that if you add 1 to its maximum value, then you know exactly what value it will have afterward on all...
19
by: Jim | last post by:
I have spent the past few weeks designing a database for my company. The problem is I have started running into what I believe are stack overflow problems. There are two tab controls on the form...
4
by: Chua Wen Ching | last post by:
Thanks Derek... Okay i had another question.. my program runs smoothly for the first minute, after 1 minute... suddenly it breaks and display this error: do you know what is the cause of...
25
by: junky_fellow | last post by:
Is there any way by which the overflow during addition of two integers may be detected ? eg. suppose we have three unsigned integers, a ,b, c. we are doing a check like if ((a +b) > c) do...
8
by: starffly | last post by:
In my program, the caculated value is supposed to be no more than the constant named MAXINT,otherwise, overflow error will be informed.however, I cannot test if the value exceeds MAXINT within the...
10
by: Parachute | last post by:
Using Borland Builder 6, I get numerical overflow when running a small programme without CodeGuard ("exp: OVERFLOW error"). The programme does not give any error messages when CodeGuard is...
1
by: zaneh | last post by:
I have found TheScripts a very useful site, but however I cannot find an answer to a problem I am having, so I finally have to ask the question! I am getting an Overflow error in a Function I have...
3
by: MLH | last post by:
A wide comma delimited text file import into A97 failed with an overflow error. I'm estimating it to have been no more than 1000 records. 100% of all the fields were quoted strings or ZLS's. ...
3
by: jer006 | last post by:
Hi I am writing a select statement that has an arithmetic function inside a case statement that uses logic to decide whether to divide or multiply and when I run the arithmetic statements outside...
42
by: thomas.mertes | last post by:
Is it possible to use some C or compiler extension to catch integer overflow? The situation is as follows: I use C as target language for compiled Seed7 programs. For integer computions the C...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.