U
user923005
Bartc said:user923005 wrote: [...]Maybe something like this:
#include <float.h>
#include <math.h>
/* 'Close enough' comparison: */
int double_compare(double d1, double d2)
{
if (d1 > d2)
if ((d1 - d2) < fabs(d1 * DBL_EPSILON))DBL_EPSILON is the smallest possible step for a floating point value? Within
the context of CAD, I would use a tolerance perhaps a million times bigger.
At least.
DBL_EPSILON is "the difference between 1 and the least value greater
than 1 that is representable in the given floating point type" (double
in this case). The actual smallest step varies tremendously depending
on the magnitude of the value -- which is why it's called *floating*
point. DBL_EPSILON is far too coarse for values near zero, and
unrepresentably fine for large values.
I haven't used fuzzy floating-point equality comparisons very often,
but it seems to me that the problem is more difficult problem than the
way it's usually presented. You need to ask yourself just what it
means for two values to be "equal", and how the values you're trying
to compare were computed; the latter can affect how much of the
difference is real, and how much is due to rounding errors. Both of
these things can vary greatly depending on the application. It's not
just about comparing numbers; you have to understand what they *mean*.
You will notice that I have scaled the delta to DBL_EPSILON mulitplied
by the magnitude of the largest value.
Further, this scaled value is compared to the difference and not to
either value.
Of course, it is possible that a larger tolerance is fine, in which
case a larger value could be substituted.