[Python-Dev] PyObject_RichCompareBool identity shortcut

Glenn Linderman v+python at g.nevcal.com
Thu Apr 28 07:06:49 CEST 2011


On 4/27/2011 8:06 PM, Stephen J. Turnbull wrote:
> Glenn Linderman writes:
>   >  On 4/27/2011 6:11 PM, Ethan Furman wrote:
>
>   >  >  Totally out of my depth, but what if the a NaN object was allowed to
>   >  >  compare equal to itself, but different NaN objects still compared
>   >  >  unequal?  If NaN was a singleton then the current behavior makes more
>   >  >  sense, but since we get a new NaN with each instance creation is there
>   >  >  really a good reason why the same NaN can't be equal to itself?
>
> Yes.  A NaN is a special object that means "the computation that
> produced this object is undefined."  For example, consider the
> computation 1/x at x = 0.  If you approach from the left, 1/0
> "obviously" means minus infinity, while if you approach from the right
> just as obviously it means plus infinity.  So what does the 1/0 that
> occurs in [1/x for x in range(-5, 6)] mean?  In what sense is it
> "equal to itself"?  How can something which is not a number be
> compared for numerical equality?
>
>   >   >>>  n1 = float('NaN')
>   >   >>>  n2 = float('NaN')
>   >   >>>  n3 = n1
>   >
>   >   >>>  n1
>   >  nan
>   >   >>>  n2
>   >  nan
>   >   >>>  n3
>   >  nan
>   >
>   >   >>>  [n1] == [n2]
>   >  False
>   >   >>>  [n1] == [n3]
>   >  True
>   >
>   >  This is the current situation: some NaNs compare equal sometimes, and
>   >  some don't.
>
> No, Ethan is asking for "n1 == n3" =>  True.  As Mark points out, "[n1]
> == [n3]" can be interpreted as a containment question, rather than an
> equality question, with respect to the NaNs themselves.

It _can_ be interpreted as a containment question, but doing so is 
contrary to the documentation of Python list comparison, which presently 
doesn't match the implementation.  The intuitive definition of equality 
of lists is that each member is equal.  The presence of NaN destroys 
intuition of people that don't expect them to be as different from 
numbers as they actually are, but for people that understand NaNs and 
expect them to behave according to their definition, then the presence 
of a NaN in a list would be expected to cause the list to not be equal 
to itself, because a NaN is not equal to itself.

> In standard
> set theory, these are the same question, but that's not necessarily so
> in other set-like toposes.  In particular, getting equality and set
> membership to behave reasonably with respect to each other one of the
> problems faced in developing a workable theory of fuzzy sets.
>
> I don't think it matters what behavior you choose for NaNs, somebody
> is going be unhappy sometimes.

Some people will be unhappy just because they exist in the language, so 
I agree :)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20110427/1a44dd56/attachment.html>


More information about the Python-Dev mailing list