Rich Comparisons Gotcha
Robert Kern
robert.kern at gmail.com
Sun Dec 7 19:14:24 EST 2008
Steven D'Aprano wrote:
> On Sun, 07 Dec 2008 23:20:12 +0000, Steven D'Aprano wrote:
>
>> On Sun, 07 Dec 2008 15:32:53 -0600, Robert Kern wrote:
>>
>>> Rasmus Fogh wrote:
>>>
>>>> Current behaviour is both inconsistent and counterintuitive, as these
>>>> examples show.
>>>>
>>>>>>> x = float('NaN')
>>>>>>> x == x
>>>> False
>>> Blame IEEE for that one. Rich comparisons have nothing to do with that
>>> one.
>> There is nothing to blame them for. This is the correct behaviour. NaNs
>> should *not* compare equal to themselves, that's mathematically
>> incoherent.
>
> Sorry, I should explain why.
>
> Given:
>
> x = log(-5) # a NaN
> y = log(-2) # the same NaN
> x == y # Some people want this to be true for NaNs.
>
> Then:
>
> # Compare x and y directly.
> log(-5) == log(-2)
> # If x == y then exp(x) == exp(y) for all x, y.
> exp(log(-5)) == exp(log(-2))
> -5 == -2
>
>
> and now the entire foundations of mathematics collapses into a steaming
> pile of rubble.
I didn't mean to suggest that it was incorrect, just that that particular
surprising behavior is not related to rich comparisons. Even if the OP gets an
__equals__() or some such, NaN will still not compare equal to NaN.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list
mailing list