Il problema nella comparazione classica
if ( fabs(a-b) <= Machine_Epsilon * fabs(a))
è che non funziona se a è uguale zero:
From:
[email protected] (Hans-Bernhard Broeker)
Subject: Re: Comparison of 2 floating point values
Date: 21 Dec 1999 00:00:00 GMT
Message-ID: <
[email protected]>
References: <
[email protected]> <
[email protected]> <
[email protected]> <
[email protected]>
Followup-To: comp.graphics.algorithms,comp.graphics.api.opengl
X-Complaints-To:
[email protected]
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 (
[email protected]) 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 (
[email protected])
Even if all the snow were burnt, ashes would remain.
poi ci sono le funzioni proposte da Matt Austern che è pur sempre un'autorità.
Ops scusatemi,non sono di Matt Austern(magari);la risposta è di Matt Austern
http://cpptips.hyperformix.com/cpptips/fp_eq
Avevo pensato di mettere tutto in un header in modo da avere a disposizione sempre
le capacità della macchina:
#include <limits>
...
numeric_limits<double> double_info;
const double EPS=double_info.epsilon();
...
ma dice che non è l'ideale se hai 50 moduli che utilizzano lo stesso header.
Che ne pensate?