2

I am curious to know why

float('nan') == float('nan') 

returns False.

I noticed this while debugging and realizing that if I had foo = float('nan') then to my surprise foo in [bar, float('nan')] always returned False.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Tommy
  • 628
  • 11
  • 22
  • 8
    Because that's one of the fundamental properties of nan: all comparison operations always return false. – Mad Physicist Apr 04 '18 at 05:07
  • 1
    @MadPhysicist Really? Shame on me for not knowing that. – Tommy Apr 04 '18 at 05:09
  • 1
    [Here's](https://stackoverflow.com/a/1573715/5014455) an answer to the *why is that in the standard* question from someone who apparently was actually on the committee... – juanpa.arrivillaga Apr 04 '18 at 05:12
  • 1
    The _real_ answer is that the committee was full of electrical engineers who don't understand how programming works. Most languages in 1985 had comparison operators that define a total ordering—possibly as a partial function that's a total ordering where it's defined (in Python terms, that means they either order things sensibly or raise an exception)—but definitely not a total function that's a partial ordering. So "signaling NaN" makes some sense, but "quiet NaN' comparisons do not. Unfortunately, in 2018, the general rule is even more widespread than in 1985, but a violation for NaN is too. – abarnert Apr 04 '18 at 05:19

0 Answers0