Rich Comparisons Gotcha
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
>> Blame IEEE for that one. Rich comparisons have nothing to do with that
> There is nothing to blame them for. This is the correct behaviour. NaNs
> should *not* compare equal to themselves, that's mathematically
Sorry, I should explain why.
x = log(-5) # a NaN
y = log(-2) # the same NaN
x == y # Some people want this to be true for NaNs.
# 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.
More information about the Python-list