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

Alexander Heger python at 2sn.net
Fri Jan 9 14:05:44 CET 2015


Dear Chris,

thanks for answering my questions.

>> 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.
>
> Why should math.nan be a singleton? Consider:
>
>>>> float("1.0") is float("1.0")
> False
>>>> (float("1.0"),) == (float("1.0"),)
> True
>
> Even with integers, although CPython caches some small ones:
>
>>>> int("12345") is int("12345")
> False
>>>> (int("12345"),) == (int("12345"),)
> True
>
> (And CPython also caches literals, so both these tests have to use
> explicit constructor calls.)
>
> I think we can all agree that the number 12345 is unique - there
> aren't two such numbers. So if we're allowed to have two int objects
> representing the exact same number, why should NaN (which represents
> the vastly infinite space of non-numbers) be unique?

I think I was not thinking clearly.
Here, if float('nan') would return math.nan it would it would, by
definition, always be the same object instead of creating new ones
(with different payloads). An issue may be if the value comes from
elsewhere, e.g., was pickled.

I am now wondering what about float("-nan")?  Do you want to introduce
a syntax for signaling nans and such with payload and signs
float("-nans123")?

-Alexander


More information about the Python-ideas mailing list