# Thread: Python large numbers

1. Frothy Coffee!
Join Date
Jun 2010
Location
/home/TX
Beans
225
Distro
Ubuntu 12.04 Precise Pangolin

## Python large numbers

Hello Ubuntu Forums community,

I am making a python program that requires the math.pow function. I need it to go up to 2^31 (2 to the 31st power) and preferably even farther. When I run the program in the terminal, it says "OverflowError: math range error"

Any way to fix this?
Thanks in advance.

2. ## Re: Python large numbers

What are you doing, exactly? math.pow(2, 32) doesn't raise an exception for me.

3. ## Re: Python large numbers

Python supports arbitrary precision numbers out of the box. What version are you using? And can you post the code that seems to be troublesome?

4. ## Re: Python large numbers

Originally Posted by Queue29
Python supports arbitrary precision numbers out of the box. What version are you using? And can you post the code that seems to be troublesome?
thats not right. its just long ints that have infinite range.

http://docs.python.org/library/stdty...t-long-complex

Code:
```>>> math.sin(pow(2,1000))
-0.15920170308624243
>>> math.sin(pow(2,10000))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to float```

5. ## Re: Python large numbers

Originally Posted by ssam
thats not right. its just long ints that have infinite range.

http://docs.python.org/library/stdty...t-long-complex

Code:
```>>> math.sin(pow(2,1000))
-0.15920170308624243
>>> math.sin(pow(2,10000))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to float```
I thought it would calculate pow as floating point number anyway... what on Earth is Python doing... 10000 multiplications?

Anyway as far as I can tell most languages use floating point as provided by their processor which is usually IEEE-754. I think it give about 16 digits of precision and exponent up to something like 300 in decimal.

6. ## Re: Python large numbers

Originally Posted by worksofcraft
I thought it would calculate pow as floating point number anyway... what on Earth is Python doing... 10000 multiplications?
pow is integer, math.pow is float.

Code:
```>>> pow(2, 1000)
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376L
>>> math.pow(2, 1000)
1.0715086071862673e+301```
Incredible as it may sound, having integer powers is actually very useful. When doing arithmetic for example.

7. Frothy Coffee!
Join Date
Jun 2010
Location
/home/TX
Beans
225
Distro
Ubuntu 12.04 Precise Pangolin

## Re: Python large numbers

Thanks Bachstelze. I didn't know there was just pow() and that there was a difference between pow() and math.pow().

Marking thread as solved.

8. ## Re: Python large numbers

Originally Posted by kroq-gar78
Thanks Bachstelze. I didn't know there was just pow() and that there was a difference between pow() and math.pow().

Marking thread as solved.
Yup, this is why you should never do

Code:
`from math import *`

9. ## Re: Python large numbers

Originally Posted by Bachstelze
pow is integer, math.pow is float.
Well, unless you give pow float parameters.

That math.pow is specifically a float is interesting; could be one of these rare cases where I might actually agree with worksofcraft's objections to Python's handling of number types.

10. ## Re: Python large numbers

Originally Posted by CptPicard
Well, unless you give pow float parameters.

That math.pow is specifically a float is interesting; could be one of these rare cases where I might actually agree with worksofcraft's objections to Python's handling of number types.
It makes sense to me. As I said, in some applications you really want full-precision exponentiation of integers (e.g. when doing RSA crypto), but on the other hand, sometimes you don't need the precision, and having long integers is just a bother.

Having both is a nice thing about Python IMO.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•