开发者

is OverflowError actually raised?

开发者 https://www.devze.com 2023-03-29 09:30 出处:网络
According to the python documentation exception OverflowError Raised when the result of an arithmetic operation is too large to

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消