float("nan") in set or as key

Gregory Ewing greg.ewing at canterbury.ac.nz
Thu Jun 2 19:17:17 EDT 2011


Steven D'Aprano wrote:

> def kronecker(x, y):
>     if x == y: return 1
>     return 0
> 
> This will correctly consume NAN arguments. If either x or y is a NAN, it 
> will return 0.

I'm far from convinced that this result is "correct". For one
thing, the Kronecker delta is defined on integers, not reals,
so expecting it to deal with NaNs at all is nonsensical.
For another, this function as written is numerically suspect,
since it relies on comparing floats for exact equality.

But the most serious problem is, given that

> NAN is a sentinel for an invalid operation. NAN + NAN returns a NAN 
> because it is an invalid operation,

if kronecker(NaN, x) or kronecker(x, Nan) returns anything
other than NaN or some other sentinel value, then you've
*lost* the information that an invalid operation occurred
somewhere earlier in the computation.

You can't get a valid result from data produced by an
invalid computation. Garbage in, garbage out.

> not because NANs are magical goop that spoil everything they touch.

But that's exactly how the *have* to behave if they truly
indicate an invalid operation.

SQL has been mentioned in relation to all this. It's worth
noting that the result of comparing something to NULL in
SQL is *not* true or false -- it's NULL!

-- 
Greg



More information about the Python-list mailing list