[Python-Dev] cmp(x,x)

Gustavo Niemeyer niemeyer at conectiva.com
Tue May 18 19:32:00 EDT 2004


> > This is odd. We'll have problems trying to implemnt a NaN
> > following the convention of being not-equal to itself,
> > for example.
> 
> I know the issue is a bit obfuscated, but please do read my mail
> entierely first: this is exactly my example :-)

Sorry.. I read your mail in a rush, and the main issue looked
to be related to these examples. Here we go again.

[...]
> >>> f = float('nan')
> >>> cmp(f,f)
> 0                    # because f is f
> >>> f == f
> False                # because float.__eq__() is called

I don't get the same results:

>>> f = float('nan')
>>> cmp(f,f)
0
>>> f == f
True

Also on a Linux box.

> Note that as discussed below the following behavior is *expected* and in
> accordance with standards:
> 
> >>> float('nan') is float('nan')
> False

I belive that testing identity on NaN might return True in cases like:

>>> f = float('nan')
>>> f is f
True

> >>> float('nan') == float('nan')
> False                             # not the same object

This is expected in an IEEE754 compliant implementation. Something
we currently are not (I get different results, as shown).

> Unless there are serious objections I suggest to (i.e. I plan to) remove the
> short-cut in PyObject_RichCompareBool() -- performance is probably not an
> issue here -- and then review all built-in comparison methods and make sure
> that they return "equal" for identical objects.

+1

-- 
Gustavo Niemeyer
http://niemeyer.net



More information about the Python-Dev mailing list