Why float('Nan') == float('Nan') is False

Rob Gaddi rgaddi at highlandtechnology.invalid
Wed Feb 13 16:07:03 EST 2019


On 2/13/19 12:32 PM, Marko Rauhamaa wrote:
> "Avi Gross" <avigross at verizon.net>:
> 
>> A NaN is a bit like a black hole. Anything thrown in disappears and
>> that is about all we know about it. No two black holes are the same
>> even if they seem to have the same mass, spin and charge. All they
>> share is that we don't know what is in them.
> 
> Then, how do you explain:
> 
>     >>> float("nan") != float("nan")
>     True
> 
> Why's that not False?
> 
> 
> Marko
> 

Because IEEE-754 decided that it was non-optional that (x != y) was 
equal to not (x == y).  Which is not the case for the ordering 
operators, since ordering is inherently undefined.

In part, these decisions were made to make it possible to detect a NaN 
in C in the absence of an isnan() function.  If (x != x), then x must be 
a NaN.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.



More information about the Python-list mailing list