float("nan") in set or as key

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Jun 3 22:21:31 EDT 2011


On Sat, 04 Jun 2011 12:14:03 +1200, Gregory Ewing wrote:

> Steven D'Aprano wrote:
>> Fair point. Call it an extension of the Kronecker Delta to the reals
>> then.
> 
> That's called the Dirac delta function, and it's a bit different 

Yes, I'm familiar with the Dirac delta. As you say, it's not really 
relevant to the question on hand.

In any case, my faux Kronecker was just a throw away example. If you 
don't like it, throw it away! The specific example doesn't matter, since 
the principle applies: functions may throw away NANs if they are not 
relevant to the calculation. The presence of a NAN is not intended to be 
irreversible, merely *usually* irreversible.


[...]
>> y = 2.1e12
>> if abs(x - y) <= 1e-9:
>>     # x is equal to y, within exact tolerance ...
> 
> If you expect your numbers to be on the order of 1e12, then 1e-9 is
> obviously not a sensible choice of tolerance. You don't just pull
> tolerances out of thin air, you justify them based on knowledge of the
> problem at hand.

Exactly. But that's precisely what people do! Hence my comment (which you 
snipped) about people feeling virtuous because they avoid "testing floats 
for equality", but then they go and do an operation like the above.

I'm sure you realise this, but for anyone reading who doesn't understand 
why the above is silly, there are no floats less than 1e-9 from y above.



-- 
Steven



More information about the Python-list mailing list