According to the python documentation
exception OverflowError
Raised when the result of an arithmetic operation is too large to
be represented. This cannot occur for long integers (which would
rather raise MemoryError than give up) and for most operations with
plain integers, which return a long integer instead. Because of the
lack of standardization of floating point exception handling in C,
most floating point operations also aren’t checked.
Indeed, this error made sense when overflowing integers were not converted to long automatically. Similarly, floats overflow to inf
. I don't really see any situation where the standard interpreter may still raise OverflowError. Is there such a case somewhere ? Just a curiosity.
In Python, OverflowError occurs when any operations like arithmetic operations or any other variable storing any value above its limit then there occurs an overflow of values that will exceed it's specified or already defined limit. In general, in all programming languages, this overflow error means the same.
The Python "OverflowError: math range error" occurs when the result of a mathematical calculation is too large. Use a try/except block to handle the error or use the numpy module if you have to manipulate larger numbers.
When an arithmetic operation exceeds the limits of the variable type, an OverflowError is raised. Long integers allocate more space as values grow, so they end up raising MemoryError. Floating point exception handling is not standardized, however. Regular integers are converted to long values as needed.
The Python float does not have sufficient precision to store the + 2 for sys. float_info. max, therefore, the operations is essentially equivalent to add zero. Also show that adding the maximum 64 bits float number with itself results in overflow and that Python assigns this overflow number to inf.
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> float(10**1000)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to float
Come to think of it (I think I saw the first one in a comment which has disappeared, so I'm not sure who to credit):
>>> 10.0**1000
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: (34, 'Result too large')
>>> 10j**1000
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: complex exponentiation
These are all of the x-to-int-power or int-to-float (or complex works too) type.
And -- because it showed up on the right in the related questions! -- there's:
>>> xrange(10**100)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long
which is of a different kind.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With