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

Pierre B. pierrebai at hotmail.com
Thu Mar 25 15:53:59 CET 2010


Mark Dickinson <dickinsm <at> gmail.com> writes:

> 
> On Thu, Mar 25, 2010 at 12:39 PM, Jesus Cea <jcea <at> jcea.es> wrote:
> >
> > But IEEE 754 was created by pretty clever guys and sure they had a
> > reason for define things in the way they are. Probably we are missing
> > something.
> 
> Well, if we are, then nobody seems to know what!  See the Bertrand
> Meyer blog post that was linked to up-thread.

The missing part, IMO, is that allowing a given NaN value to compare equal to
itself only pushes the problem up one level. Any single operation yielding a
NaN will still be unequal to itself. That is, under what is being proposed,
with a function func() returning the same result of some calculation:

x = func()
s1 = (x)
print x in s1
print func() in s1

This would print True and False, even though func() is perfoming the same
calculation and thus logically returning the same NaN.

I think the IEEE NaN represent the fact that you have a number of an undefined
set, but it doesn't specify which.

The only way out, IMO, is to make *all* NaN comparisons yield False, but
identity yield true. No interning necessary. At most, you could make the
identity function return False for the different types of NaN.




More information about the Python-Dev mailing list