Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing "float('nan')" and "math.nan"

I have a float variable which may or may not be a number, and I want to check if that is the case. With x = float('nan'), I observed some behavior that surprised me:

    print(x is math.nan)
>>> False

This means that float('nan') and math.nan are different objects, which I didn't expect, but that's okay. However, the result is the same, when I check for equality with ==:

print(x == math.nan):
>>> False

I get the correct result for all kinds of not-a-number, if I use math.isnan(x). Still, why doesn't float('nan') == math.nan evaluate to True?.

like image 357
Frank from Frankfurt Avatar asked Oct 24 '25 02:10

Frank from Frankfurt


2 Answers

"Not a number" is (in some sense) the absence of a value.

Traditionally, and per the IEEE floating-point specification, it does not equal itself.

That's because there is no meaningful value to compare.

In fact, some people use this fact to detect NaN, so you could try x != x as your condition instead (though the linked Q&A arguably has some better suggestions).

The expression math.nan is math.nan is true, though, because is does an object identity comparison rather than a value equivalence/equality comparison.

like image 51
Lightness Races in Orbit Avatar answered Oct 26 '25 23:10

Lightness Races in Orbit


This is not special behaviour: is returns whether two object are actually referring to the same thing (essentially in memory) and == returns whether two objects have the same value.

To see if they refer to the same thing, we can use id().

>>> a = [1,2,3]
>>> b = a
>>> id(a)
140302781856200
>>> id(b)
140302781856200
>>> a == b
True
>>> a is b
True
>>> c = [1,2,3]
>>> id(c)
140302781864904
>>> a == c
True
>>> a is c
False

Here we see that by assigning b = a, they now refer to the same list: hence is and == are True. However when we define c to be a new variable with the same value as a and b, it is ==, but is returns False.

The same is true for NaNs.

like image 20
Joe Iddon Avatar answered Oct 26 '25 23:10

Joe Iddon