From: broeker@acp3bf.knirsch.de (Hans-Bernhard Broeker) Subject: Re: Comparison of 2 floating point values Date: 21 Dec 1999 00:00:00 GMT Message-ID: <83ntdo$p9h@acp3bf.knirsch.de> References: <83luop$8p0$1@news5.svr.pol.co.uk> <83m3lb$1v9$1@equila.ntrl.net> <385ebb9c_2@206.102.135.5> Followup-To: comp.graphics.algorithms,comp.graphics.api.opengl X-Complaints-To: abuse@rwth-aachen.de X-Trace: nets3.rz.RWTH-Aachen.DE 945780991 21761 137.226.32.75 (21 Dec 1999 12:56:31 GMT) Organization: RWTH Aachen, III. physikalisches Institut B NNTP-Posting-Date: 21 Dec 1999 12:56:31 GMT Newsgroups: comp.graphics.algorithms,comp.graphics.api.opengl Paul Hsieh (DONT.qed.SPAM.ME@pobox.com) wrote: > Usually to compare two numbers assuming a certain amount accuracy you do > it like this: > if( fabs(a/b-1) < MachEps ) Caution with this; you may get divide by zero problems. Avoid division, like this: if ( fabs(a-b) <= Machine_Epsilon * fabs(a)) Which is what the comp.lang.c FAQ recommends. And even _that_ doesn't work if a is zero... you'ld need a special case, like: #define EQ(a,b,precision) \ (fabs(a-b) <= precision * (a==0? (Machine_Epsilon*fabs(a)) \ : (Machine_Smallest))) Where 'precision' is a factor that describes how much precision has already been lost, relative to what the machine floating point format can describe. This has to be a factor bigger than 1, to make sense. It's this 'precision' factor you have to hand-tune to for every single application of such a comparison, on every platform. The latter is because comparing to a relative difference of 'Machine_Epsilon' is not a useful thing. By definition of Epsilon and the structure of floating point numbers, it is roughly the smallest *relative* difference between any two FP numbers. I.e. two numbers with a relative difference of less than Epsilon would be exactly equal, already, not just approximately, and the whole point of using a test other than simple '==' would be moot. As I hope you can see, dealing with floating point numbers can be surprisingly complicated, as soon as you get down to the gory details. -- Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de) Even if all the snow were burnt, ashes would remain.