-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Open
Labels
Description
Description
nans (like float("nan"), np.nan) follow the IEEE 754 standard for floating-point arithmetic, which specifies that NaN is not equal to anything, including itself, so:
np.nan == np.nan # False
float("nan") == float("nan") # FalseBut
np.nan is np.nan # TrueSo these are objects that are not equal but identical to themselves. This leads to unexpected results in cases where object identity is taken as a short cut before equality comparisons, e.g. in tuples.
Snippet preview
>>> import numpy as np
>>> np.nan == np.nan
False
>>> (np.nan, np.nan) == (np.nan, np.nan)
True
>>> nan1 = float('nan'); nan2 = float('nan')
>>> nan1 == nan2
False
>>> (nan1, nan2) == (nan1, nan2)
True
>>> (nan1, nan2) == (nan2, nan1)
FalseChecklist before calling for maintainers
- Have you checked to ensure there aren't other open Issues for the same update/change?
- Have you checked that this snippet is not similar to any of the existing snippets?
- Have you added an
Explanationsection? It shall include the reasons for changes and why you'd like us to include them
sebix