Hashing & equality of sequences containing nan

Thanks for explaining the value interning. Yes, it is a common source of under-specified behaviour and I should have remembered that.

Reading that made me realize I was making some assumptions about nan being singleton.
Now I see the discussion on Making NaN a singleton

And the below shows that nan is not singleton on CPython.

$ python3
Python 3.10.8 (main, Oct 13 2022, 10:17:43) [Clang 14.0.0 (clang-1400.0.29.102)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from math import nan, isnan, acos
>>> nan0 = nan
>>> try:
...   nan1 = acos(2.0)
... except:
...   nan1 = float('nan')
...
>>> nan0 is nan1
False
>>> isnan(nan0)
True
>>> isnan(nan1)
True
>>> s = set((nan0, nan1))
>>> len(s)
2
1 Like