Float equality? (was: [BUG] Float NaN's)

Jarvis, Robert P. (Bob) (Contingent) bob.jarvis at timken.com
Tue Sep 14 18:49:56 UTC 2004


I'm confused, Tim.  As best I can remember, floating point numbers can
reasonably be compared to one another using inequalities.  My understanding
is that it's *equality* comparisons that are often misleading and should be
avoided.  If I've misunderstood your point please help me understand.
Thanks.

Bob Jarvis
Compuware @ Timken

> -----Original Message-----
> From: Tim Rowledge [mailto:tim at sumeru.stanford.edu]
> Sent: Tuesday, September 14, 2004 2:33 PM
> To: squeak-dev at lists.squeakfoundation.org
> Subject: RE: Float equality? (was: [BUG] Float NaN's)
> 
> 
> "Jarvis, Robert P. (Bob) (Contingent)" <bob.jarvis at timken.com> wrote:
> 
> > The best practice I'm aware of for handling equality 
> calculations with
> > Floats is avoid them completely.  You should establish what 
> you consider to
> > be an acceptable epsilon value based on your understanding 
> of your data and
> > use it as follows:
> > 
> > 	maxEpsilon = 0.000001.
> > 		.
> > 		.
> > 		.
> > 	(f1 - f2) abs < maxEpsilon
> > 		ifTrue: ["f1 and f2 are approximately equal"]
> > 		ifFalse: ["f1 and f2 are not approximately equal"]
> Argh! Bob, you just broke your own advice. Is that a dagger, er float
> comparison, I see before me?
> 
> Worse yet, to be meaningful the epsilon probably ought to be a
> percentage/fraction and not an absolute value since the precision of
> Floats is different to the range. (Apologies if I'm not using the
> currently fashonable terms). So, if we postulate an extremely limited
> form of Float with only 4sf we would perhaps do
> (f1 / f2 < 1.0001 and:[f2 / f1 <1.0001])
> 	ifTrue:[approx equal]
> 	ifFalse[not really equal]
> And of course it still uses a comparison. :-(
> 
> Like you said, don't use'em if precision is important. In fact, don't
> ese them at all. Then we could use all that silicon on the cpu for
> something more useful.
> 
> 
> tim
> --
> Tim Rowledge, tim at sumeru.stanford.edu, http://sumeru.stanford.edu/tim
> Strange OpCodes: HSJ: Hop, Skip and Jump
> 

-----------------------------------------
**********************************************************************
PLEASE NOTE: The above email address has recently changed from a previous
naming standard -- if this does not match your records, please update them
to use this new name in future email addressed to this individual.    This
message and any attachments are intended for the   individual or entity
named above. If you are not the intended  recipient, please do not forward,
copy, print, use or disclose this   communication to others; also please
notify the sender by   replying to this message, and then delete it from
your system.     The Timken Company
**********************************************************************




More information about the Squeak-dev mailing list