Rich Comparisons Gotcha

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Dec 8 00:37:32 CET 2008


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.


-- 
Steven



More information about the Python-list mailing list