float("nan") in set or as key

MRAB python at mrabarnett.plus.com
Sun May 29 13:44:08 EDT 2011


On 29/05/2011 15:41, Grant Edwards wrote:
> On 2011-05-29, Wolfgang Rohdewald<wolfgang at rohdewald.de>  wrote:
>> On Sonntag 29 Mai 2011, Tim Delaney wrote:
>>> There's a second part the mystery - sets and dictionaries (and
>>> I think lists) assume that identify implies equality (hence
>>> the second result). This was recently discussed on
>>> python-dev, and the decision was to leave things as-is.
>>
>> On Sonntag 29 Mai 2011, Grant Edwards wrote:
>>> Even if they are the same nan, it's still not equal to itself.
>>
>> if I understand this thread correctly, they are not equal to itself
>> as specified by IEEE
>
> And Python follows that convention.
>
>> but Python treats them equal in sets and dictionaries for performance
>> reasons
>
> It treats them as identical (not sure if that's the right word).  The
> implementation is checking for ( A is B or A == B ).  Presumably, the
> assumpting being that all objects are equal to themselves.  That
> assumption is not true for NaN objects, so the buggy behavior is
> observed.
>
Would there be any advantage to making NaN a singleton? I'm thinking
that it could make checking for it cheaper in the implementation of
sets and dicts. Or making NaN unhashable?



More information about the Python-list mailing list