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

Alexander Heger python at 2sn.net
Fri Jan 9 12:58:19 CET 2015


here another odd issue related to NaN arithmetics:

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

this is what we want.  Good. But

(2)
>>> x = (nan,)
>>> x is x
True
>>> x == x
True

... hmm ... apparently the comparison uses ID before using == ? for
tuple elements?

on the other hand

(3)
>>> (nan,) is (nan,)
False
>>> (nan,) == (nan,)
True

I guess we see from

(4)
>>> float('nan') is float('nan')
False
>>> (float('nan'),) == (float('nan'),)
False

I understand why they all give the results they do, however, I think
the second comparison in (2) should give False and, more importantly,
the first comparison of (4) should be True - maths.nan should be a
singleton - even if we allow to float.make_nan() to produce NaNs with
different payloads - but maths.nan could be just one thing, one
instance, the numerical counterpart to None.

-Alexander


More information about the Python-ideas mailing list