Python: Error 34: Integer overflow (beginner)
Question:
I have to handle very big integers in my program, but I get the following error:
Traceback (most recent call last):
File "[path]", line n, in <module>
number = int(numbers[0]*(10**numbers[1]))
OverflowError: (34, 'Numerical result out of range')
for number = int(n)
when I entered 8e10000000 as n.
Is there a way to solve this problem?
Thanks in advance.
Answers:
The number 8e10000000
is not an integer, it is a floating point number to Python. Any number using the e
notation is treated as a float. Python uses (usually) a 64-bit float format, and that cannot hold such a large number.
So the problem is not the integer, it is the float you start with. The error is not at the line number = int(n)
, it is at the line n = 8e10000000
or whatever equivalent you used.
You can avoid that error by using
n = 8 * 10**10000000
This results in an integer. But be careful–that takes a lot of time and memory to build the integer in RAM. (My system took 19 seconds to execute that one command.) And if you try to print that value, the computer will take a very long time and a large amount of memory to build up the string value to be printed.
Finally, as others have pointed out, that statement that you claim does not match the error message. So it is possible that something else is going on. If you want closure from us, show an entire code snippet that shows that error.
8e10000000
is very large number, and Python it represents as a float.
CPython usually store this float in 64-bit size, which is too small for such a big number.
For such large numbers is safe to use Decimal
module:
import sys
from decimal import Decimal
print('max size = ', sys.maxsize)
number = Decimal("8e10000000")
print(number)
Outputs:
max size = 9223372036854775807
8E+10000000
The number 9223372036854775807 is exactly 2^63 – 1.
What you’re running into is an issue where python is Strongly Typed but also Dynamically Typed. 8e10000000 is actually of the python (and C) type float
and is a valid value for a double precision floating point binary representation, whereas the maximum valid value for a python int
is 9,223,372,036,854,775,807 (found with sys.maxint
).
So, python has a decimal
library that has a class decimal.Decimal
documentation here that does arbitrary precision numbers. It’s not going to be as memory or speed efficient, but it bypasses the size-limit and precision issues that floating point numbers have, and are particularly when dealing with money.
The other option that you can consider if you are truly using inter values is to use long(n)
to cast your variable to an arbitrarily large integer in python 2.5+ (in python 3.0+ int
is a long
) here’s a link to the PEP 237 that talks about it.
I have to handle very big integers in my program, but I get the following error:
Traceback (most recent call last):
File "[path]", line n, in <module>
number = int(numbers[0]*(10**numbers[1]))
OverflowError: (34, 'Numerical result out of range')
for number = int(n)
when I entered 8e10000000 as n.
Is there a way to solve this problem?
Thanks in advance.
The number 8e10000000
is not an integer, it is a floating point number to Python. Any number using the e
notation is treated as a float. Python uses (usually) a 64-bit float format, and that cannot hold such a large number.
So the problem is not the integer, it is the float you start with. The error is not at the line number = int(n)
, it is at the line n = 8e10000000
or whatever equivalent you used.
You can avoid that error by using
n = 8 * 10**10000000
This results in an integer. But be careful–that takes a lot of time and memory to build the integer in RAM. (My system took 19 seconds to execute that one command.) And if you try to print that value, the computer will take a very long time and a large amount of memory to build up the string value to be printed.
Finally, as others have pointed out, that statement that you claim does not match the error message. So it is possible that something else is going on. If you want closure from us, show an entire code snippet that shows that error.
8e10000000
is very large number, and Python it represents as a float.
CPython usually store this float in 64-bit size, which is too small for such a big number.
For such large numbers is safe to use Decimal
module:
import sys
from decimal import Decimal
print('max size = ', sys.maxsize)
number = Decimal("8e10000000")
print(number)
Outputs:
max size = 9223372036854775807
8E+10000000
The number 9223372036854775807 is exactly 2^63 – 1.
What you’re running into is an issue where python is Strongly Typed but also Dynamically Typed. 8e10000000 is actually of the python (and C) type float
and is a valid value for a double precision floating point binary representation, whereas the maximum valid value for a python int
is 9,223,372,036,854,775,807 (found with sys.maxint
).
So, python has a decimal
library that has a class decimal.Decimal
documentation here that does arbitrary precision numbers. It’s not going to be as memory or speed efficient, but it bypasses the size-limit and precision issues that floating point numbers have, and are particularly when dealing with money.
The other option that you can consider if you are truly using inter values is to use long(n)
to cast your variable to an arbitrarily large integer in python 2.5+ (in python 3.0+ int
is a long
) here’s a link to the PEP 237 that talks about it.