2011-08-21 13 views
5

Secondo la documentazione pythonè effettivamente rilevato 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. 

In effetti, questo errore aveva senso quando interi traboccanti non sono stati convertiti in tempo automaticamente. Allo stesso modo, float overflow su inf. Non vedo davvero alcuna situazione in cui l'interprete standard possa ancora generare OverflowError. C'è un caso del genere da qualche parte? Solo una curiosità

risposta

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 

Vieni a pensarci bene (credo di aver visto il primo in un commento che è scomparso, quindi non sono sicuro che al credito):

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

Queste sono tutte le tipo x-to-int-power o int-to-float (o anche complessi).

E - perché è apparso sulla destra nelle domande correlate! - c'è:

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

che è di un tipo diverso.

+0

ok, interessante. Mi sarei aspettato che fosse convertito in "inf" .' >>> a = 1e1000 >>> a inf' –

Problemi correlati