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

Mark Dickinson dickinsm at gmail.com
Thu Mar 25 15:42:14 CET 2010

On Thu, Mar 25, 2010 at 2:26 PM, Antoine Pitrou <solipsis at pitrou.net> wrote:
> Le Thu, 25 Mar 2010 07:19:24 -0700, Curt Hagenlocher a écrit :
>> Wait, what? I haven't been paying much attention, but this is backwards.
>> There are multiple representations of NaN in the IEEE encoding; that's
>> actually part of the problem with saying that NaN = NaN or NaN != NaN.
>> If you want to ignore the "payload" in the NaN, then you're not just
>> comparing bits any more.
> This sounds a bit sophistic, if the (Python) user doesn't have access to
> the payload anyway.

Well, you can get at the payload using the struct module, if you care
enough.  But yes, it's true that Python doesn't take much care with
the payload:  e.g., ideally, an operation on a nan (3.0 + nan,
sqrt(nan), ...) should return exactly the same nan, to make sure that
information in the payload is preserved.  Python doesn't bother, for
floats (though it does for decimal).


More information about the Python-Dev mailing list