-1
In [6]: a = float('nan')

In [7]: a == a
Out[7]: False

Why?

nichochar
  • 2,720
  • 1
  • 18
  • 16

1 Answers1

8

There are two reasons, one practical and one historical. The historical reason is that this makes it trivial to test for a NaN. If the value isn't equal to itself, it's a NaN.

The practical reason is that NaN is used to indicate various different cases where the value is not defined, and not all undefined values are equal.

Should NaN/NaN equal 1? Think about how crazy that would be. Arguing that NaN should equal itself is only a bit less crazy.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278