Why `divmod(float('inf'), 1) == (float('nan'), float('nan'))`

wxjmfauth at gmail.com wxjmfauth at gmail.com
Thu Sep 18 08:22:28 CEST 2014


Le mardi 16 septembre 2014 23:40:44 UTC+2, cool-RR a écrit :
> While debugging my code I found that the bug was because I assumed that something like `divmod(float('inf'), 1)` would be equal to `(float('inf'), float('nan'))`, while Python returns `(float('nan'), float('nan'))`. Why does Python make the division result be NaN in this case? `float('inf') / 1` is still `float('inf')`.
> 

On one side, it is very nice, Python offers this
"non finite arithmetic" (just to give a name).
But on the other side it is also a plague: one
never knows how to handle calculations properly.
In an application which pretends to be solid, it's
necessary to duplicate the checkings in a unnecessary
way.

In short, a lack of consistency.

>>> 1e300*1e300
inf
>>> exp(1e300)
Traceback (most recent call last):
  File "<eta last command>", line 1, in <module>
OverflowError: math range error
>>>

jmf



More information about the Python-list mailing list