float("nan") in set or as key

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon May 30 00:15:11 EDT 2011


On Mon, 30 May 2011 11:14:58 +1000, Chris Angelico wrote:

> So, apart from float("nan"), are there actually any places where real
> production code has to handle NaN? I was unable to get a nan by any of
> the above methods, except for operations involving inf; for instance,
> float("inf")-float("inf") == nan. All the others raised an exception
> rather than return nan.

That's Python's poor design, due to reliance on C floating point 
libraries that have half-hearted support for IEEE-754, and the 
obstruction of people who don't understand the usefulness of NANs. They 
shouldn't raise unless the caller specifies that he wants exceptions. The 
default behaviour should be the most useful one, namely quiet 
(propagating) NANs, rather than halting the calculation because of 
something which may or may not be an error and may or may not be 
recoverable.

Even Apple's Hypertalk supported them better in the late 1980s than 
Python does now, and that was a language aimed at non-programmers!

The Decimal module is a good example of what floats should do. All flags 
are supported, so you can choose whether you want exceptions or NANs. I 
don't like Decimal's default settings, but at least they can be changed.



-- 
Steven



More information about the Python-list mailing list