float("nan") in set or as key

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Jun 1 21:10:11 EDT 2011


On Wed, 01 Jun 2011 14:03:14 +0000, Grant Edwards wrote:

> On 2011-06-01, Chris Angelico <rosuav at gmail.com> wrote:
>> On Wed, Jun 1, 2011 at 12:59 PM, Carl Banks <pavlovevidence at gmail.com>
>> wrote:
>>> On Sunday, May 29, 2011 7:53:59 PM UTC-7, Chris Angelico wrote:
>>>> Okay, here's a question. The Python 'float' value - is it meant to be
>>>> "a Python representation of an IEEE double-precision floating point
>>>> value", or "a Python representation of a real number"?
>>>
>>> The former. ?Unlike the case with integers, there is no way that I
>>> know of to represent an abstract real number on a digital computer.
>>
>> This seems peculiar. Normally Python seeks to define its data types in
>> the abstract and then leave the concrete up to the various
>> implementations - note,
> 
> But, "real numbers" and "IEEE float" are so different that I don't think
> that it would be a wise decision for people to pretend they're working
> with real numbers when in fact they are working with IEEE floats.

People pretend that *all the time*.

Much of the opposition to NANs, for example, is that it violates 
properties of the reals. But so do ordinary floats! People just pretend 
otherwise.

For reals, a + b - a = b, always without exception. For floats, not so 
much.

For reals, a*(b + c) = a*b + a*c, always without exception. For floats, 
not so much.

For reals, 1/(1/x) = x, except for 0, always. For floats, not so much. 
For IEEE floats with proper support for INF, 0 is one of the cases which 
does work!

These sorts of violations are far more likely to bite you than the NAN 
boogey, that x != x when x is a NAN. But people go into paroxysms of 
concern over the violation that they will probably never see, and ignore 
the dozens that they trip over day after day.

Compiler optimizations are some of the worst and most egregious 
violations of the rule Floats Are Not Reals. Large numbers of numeric 
algorithms are simply broken due to invalid optimizations written by C 
programmers who think that because they have a high school understanding 
of real-value math they therefore understand floats.


-- 
Steven



More information about the Python-list mailing list