[Python-ideas] math.inf and math.nan constants

Alexander Heger python at 2sn.net
Wed Jan 7 19:58:03 CET 2015


> Maybe this would justify a note in the documentation, that this is one
> NaN, but there are many others that are different.
>
> As long as people know not to rely on math.nan being the same object
> or having the same bits as every nan, then there's no harm.

comparison with NaNs should be tough and can only be done with the
maths.isnan functions.

IEEE754 requires that all comparisons with NaN give false, i.e.,

nan == 0:  False
nan != 0: False

(I use in FORTRAN codes to find NaNs in old code)

Same for comparing nan to itself.

>>> x = float('nan')
>>> x is x
True
>>> x == x
False

Unless math.nan was a singleton, the "is" operator would also fail,
and even that would not compare to a nan generated in computation in
general.

But I agree, having *a* nan constant in math would be good, as long as
users know what they can do with it and what not.  It should not be a
function.

-Alexander


More information about the Python-ideas mailing list