2011-08-21 3 views
5

Selon la documentation pythonOverflowError est réellement levé?

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. 

En effet, cette erreur avait un sens quand les entiers débordantes ne sont pas convertis en temps automatiquement. De même, les flotteurs débordent à inf. Je ne vois pas vraiment de situation où l'interpréteur standard peut toujours déclencher OverflowError. Y a-t-il un tel cas quelque part? Juste une curiosité.

Répondre

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 

Venez y penser (je crois avoir vu le premier dans un commentaire qui a disparu, donc je ne sais pas qui au crédit):

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

Ce sont tous les type x-to-int-power ou int-to-float (ou complexe aussi).

Et - parce qu'il s'est montré sur la droite dans les questions connexes! - il y a:

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

qui est d'un type différent.

+0

ok, intéressant. Je m'attendais à ce qu'il soit converti en "inf". >>> a = 1e1000 >>> a inf' –

Questions connexes