Floating point equality [was Re: What exactly is "exact" (was Clean Singleton Docstrings)]

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Jul 21 02:34:23 EDT 2016


On Thursday 21 July 2016 15:28, Rustom Mody wrote:

> On Wednesday, July 20, 2016 at 11:13:05 AM UTC+5:30, Steven D'Aprano wrote:
>> On Tuesday 19 July 2016 14:58, Rustom Mody wrote:
>> 
>> > So I again ask: You say «"Never compare floats for equality" is a
>> > pernicious myth»
>> 
>> It is the word *never* which makes it superstition. If people said "Take
>> care with using == for floats, its often not what you want" I would have no
>> argument with the statement.
>> 
>> I'd even (reluctantly) accept "usually not what you want". But "never" is
>> out- and-out cargo-cult programming.
> 
> You seem to not understand the realities of teaching.
> You (teacher in general) cannot say a saga; only epigrams

Is "Don't use exact equality unless you know what you're doing" enough of an 
epigram for you?


> You cannot transmit wisdom (even if you own some) just a bit of
> savviness/cleverness.

Maybe so, but that's no excuse for transmitting outright misinformation and 
superstition. In physics, dealing with motion in the presence of energy losses 
is hard, and beginning and intermediate levels of physics will generally 
explicitly or implicitly ignore friction and air resistance. But physics 
teachers do not teach that "air resistance doesn't exist; you mustn't try to 
take friction into account". They teach that it is a simplification.




> So let me ask the question again differently:
> How many errors happen by people not using ε-neighborhood checks instead of
> == checks How many errors happen by the opposite (mis)use?

Precisely 35, and 17812, respectively.


> IOW “myth”... ok “pernicious myth” Not
> 
> BTW APL whose main domain of application is scientific chooses to enshrine
> this —equality is ε-neighborhood checking not exact equality checking — into
> its builtin ‘==’

I have a lot of respect for Ken Iverson, and a lot of admiration for language 
designers willing to experiment with alternate paradigms.

But keeping in mind that in APL, if you set ⎕ct to 0 you get an exact 
comparison, can you find any quotes from Iverson saying that you should *never* 
perform exact equality comparisons?



> And
> 
http://www.dyalog.com/uploads/documents/Papers/tolerant_comparison/tolerant_comparison.htm

Nice resource, thank you.


> ε is spelt ⎕ct (Comparison Tolerance)
> And of course == is spelt =




-- 
Steve




More information about the Python-list mailing list