[Python-Dev] Why is nan != nan?

Adam Olsen rhamph at gmail.com
Fri Mar 26 02:53:31 CET 2010

On Thu, Mar 25, 2010 at 18:57, Steven D'Aprano <steve at pearwood.info> wrote:
> Simply put: we should treat "two unclear values are different" as more
> compelling than "two unclear values are the same" as it leads to fewer,
> smaller, errors. Consider:
> log(-1) = NAN  # maths equality, not assignment
> log(-2) = NAN
> If we allow NAN = NAN, then we permit the error:
> log(-1) = NAN = log(-2)
> therefore log(-1) = log(-2)
> and 1 = 2
> But if make NAN != NAN, then we get:
> log(-1) != log(-2)
> and all of mathematics does not collapse into a pile of rubble. I think
> that is a fairly compelling reason to prefer inequality over equality.
> One objection might be that while log(-1) and log(-2) should be
> considered different NANs, surely NANs should be equal to themselves?
> -1 = -1
> implies log(-1) = log(-1)

IMO, this just shows how ludicrous it is to compare NaNs.  No matter
what we do it will imply some insane mathematical consequence implied
and code that will break.

They are, after all, an error passed silently.

Why is it complex can raise an exception when sorted, forcing you to
use a sane (and explicit) method, but for NaN it's okay to silently

Adam Olsen, aka Rhamphoryncus

More information about the Python-Dev mailing list