Sorting NaNs

Chris Angelico rosuav at gmail.com
Sat Jun 2 07:20:42 EDT 2018


On Sat, Jun 2, 2018 at 9:02 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> The violation of reflexivity is weird though :-)
>
>     x = float(NAN)
>     x == x  # returns False
>
> Don't argue, just accept it :-)

The way I learned it was: there are (of course) a finite number of bit
patterns that can represent NaN, but conceptually, there are an
infinite number of things that are not numbers, and so two different
NaN values could actually represent different non-numbers. Thus, for
consistency, we assume that no two non-numbers are equal, regardless
of trivial things like bit pattern or identity. (The IEEE standard
doesn't have anything about identity, AFAIK, so it's just the bit
pattern.)

Whether that helps or not is up to you. :) Violating reflexivity truly
is weird, especially since some comparisons are described as "x == y"
but implemented as "x is y or x == y". But trying to maintain
reflexivity would make other things even more weird...

ChrisA



More information about the Python-list mailing list