Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is OverflowError actually raised?

Tags:

python

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.

like image 924
Stefano Borini Avatar asked Aug 21 '11 12:08

Stefano Borini


People also ask

What is OverflowError in Python?

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.

How do I fix OverflowError math range error in Python?

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.

How does Python handle overflow exception?

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.

Does Python float overflow?

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.


1 Answers

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.

like image 199
DSM Avatar answered Oct 02 '22 22:10

DSM