[Newbies] Re: [BUG] Inconsistent float soustraction
johnps11 at bigpond.com
johnps11 at bigpond.com
Sun Feb 17 21:37:15 UTC 2008
Cedric,
As a follow up, when doing high precision arithmetic (in engineering,
numerical integration, weather prediction and the like) you usually have a
setup routine that does things like:
BEGIN
let a = 0.5
let b = a /2.0
while ( (a+b) <> a ) do
epsilon = b
b = b/2
endwhile
then epsilon is close to machine epsilon.
More sophisticated algorithms give more accurate values.
from that point we do indeed use ABS(a - b) < epsilon for 'close enough to
equal'. There are a huge number of arithmetic tricks used to try to keep
the accumulated rounding errors within known bounds as well.
Some calculations are very badly behaved, and there is very little we can
do to try to keep results even remotely close to accurate. Chaotic
systems are one class of problems that comes to mind (found in weather
prediction for example). Another is inverting certain matrices - the
Vandemonde matrix is a classic example that is not singular, but you get
numbers that are less than machine epsilon so you find yourself trying to
divide by machine zero (or even worse get 0/0, known in computing as NaN ,
or NotANumber), even though it can be proved that these matrices do indeed
possess inverses.
I hope this makes you aware of how nasty floats and doubles (and quad and
decwords) are in computer arithmetic. One book worth reading for a
(relatively) gentle introduction to this is "An Introduction to Numerical
Computations" by Yakowitz and Szidarovszky. A more exhaustive treatment
can be found in "The Art of Computer Programming" by Donald Knuth, but
that is not a read for the light hearted. "Programming Pearls" by Bentley
also has a few examples of how badly things can go awry if you neglect to
allow for the inherent failings of float arithmetic. The last book is
great reading for anyone interested in programming, and is an entertaining
and not too mind bending read.
I'm aware that this post is far more information than you asked for, but
is enough for you to get a real handle on this stuff if you want more
detail than "Never ever test floats for equality because it will bite you
on the arse big time, cost you your job, kill innocent people and cause
the collapse of banks, insurers and civilisation itself".
Enjoy,
John
More information about the Beginners
mailing list