float("nan") in set or as key

Erik Max Francis max at alcyone.com
Sat May 28 20:16:50 EDT 2011


MRAB wrote:
> Here's a curiosity. float("nan") can occur multiple times in a set or as 
> a key in a dict:
> 
>  >>> {float("nan"), float("nan")}
> {nan, nan}
> 
> except that sometimes it can't:
> 
>  >>> nan = float("nan")
>  >>> {nan, nan}
> {nan}

It's fundamentally because NaN is not equal to itself, by design. 
Dictionaries and sets rely on equality to test for uniqueness of keys or 
elements.

 >>> nan = float("nan")
 >>> nan == nan
False

In short, don't do that.

-- 
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
  San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis
   There was never a good war or a bad peace.
    -- Benjamin Franklin, 1706-1790



More information about the Python-list mailing list