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

Tim Rowledge tim at sumeru.stanford.edu
Tue Sep 14 18:32:42 UTC 2004


"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



More information about the Squeak-dev mailing list