2011-08-21 16 views
5

De acuerdo con la documentación de Python¿Se ha producido realmente OverflowError?

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. 

De hecho, este error tenía sentido cuando los enteros que desbordan no se convirtieron a largo automáticamente. Del mismo modo, flotan desbordamiento a inf. Realmente no veo ninguna situación en la que el intérprete estándar todavía pueda generar OverflowError. ¿Hay tal caso en alguna parte? Solo una curiosidad

Respuesta

7
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 

Ahora que lo pienso de ella (creo que vi la primera en un comentario que ha desaparecido, así que no estoy seguro de que al crédito):

>>> 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 

Estos son todos de la x-to-int-power o int-to-float (o trabajos complejos también) escribe.

Y - ¡porque apareció a la derecha en las preguntas relacionadas! - hay:

>>> xrange(10**100) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
OverflowError: Python int too large to convert to C long 

que es de un tipo diferente.

+0

bien, interesante. Hubiera esperado que se convirtiera a "inf" .' >>> a = 1e1000 >>> a inf' –

Cuestiones relacionadas