Why float('Nan') == float('Nan') is False
Chris Angelico
rosuav at gmail.com
Wed Feb 13 14:57:36 EST 2019
On Thu, Feb 14, 2019 at 6:40 AM Schachner, Joseph
<Joseph.Schachner at teledyne.com> wrote:
>
> Because all comparisons with NAN return false, that's the spec.
Apart from !=, because it would be insane (I mean, even more insane
than it is) to have nan == nan be false AND nan != nan.
IEEE NAN has several purposes, including representing the concept of
"could be any number but we have no idea what", and also the concept
of "there is truly no value here". And sometimes both at once -
imagine getting per-capita statistics partitioned by age group and
postal code, and having nobody in a particular age/postcode, so you
end up with both "there is no value here" and "what do you get when
you divide zero by zero" (zero whatevers divided by zero people). So
there ARE some operations involving nan that produce real results:
>>> 1 ** nan
1.0
but as a general rule, you have to assume that nan truly isn't a number.
Oh, and if you want TRULY mind-melty fun, look into the SQL "NULL"
value, which is a value, except when it isn't. Comparisons with NULL
don't return false, they return NULL. Most of the time, if you take a
column function over a nullable column (say, taking the sum or average
of the values in a field), you just ignore any rows that are NULL; but
if *every* value is NULL, the sum is not 0, but NULL.
Now try mapping SQL's NULL to Python's float("nan"), and performing
operations on both sides.
Endless fun.
ChrisA
More information about the Python-list
mailing list