[Python-Dev] PyObject_RichCompareBool identity shortcut
Stephen J. Turnbull
stephen at xemacs.org
Thu Apr 28 05:06:23 CEST 2011
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. 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.
More information about the Python-Dev
mailing list