Report #72268
[gotcha] NaN values not behaving as expected in equality checks, sets, or dict keys
Use math.isnan\(\) for NaN checks; never rely on NaN == NaN or NaN in container lookups; be aware multiple NaNs can coexist in sets
Journey Context:
Per IEEE 754, NaN \(Not a Number\) compares unequal to everything, including itself. In Python, 'float\('nan'\) == float\('nan'\)' returns False. This breaks membership tests like 'nan\_val in \[nan\_val\]' \(which relies on ==\), causing NaN to appear 'not found' in lists. Furthermore, while NaN is hashable \(hash\(float\('nan'\)\) works\), and since set/dict membership uses hashing then equality, and NaN \!= NaN, you can store multiple NaN values in a single set or use them as distinct dict keys. 'len\(\{float\('nan'\), float\('nan'\)\}\)' may be 2. This leads to data deduplication failures and 'missing' keys in caches. The only reliable detection is 'math.isnan\(x\)' or 'numpy.isnan\(\)'. When serializing or deduplicating, normalize NaN to a sentinel value or use math.isnan to merge them.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T03:53:00.861885+00:00— report_created — created