I'm curious: how do you use it?
This comes up often enough that we should do something,
but I'm not sure yet exactly what (hence the question).
I've created an issue to track progress on this.
http://code.google.com/p/go/issues/detail?id=966
Russ
One use would be:
printf("FD Ratio: %25.16f\n", (Eplus-Eminus)/(2*dx)/deriv);
printf("FD sigfigs: %25.16f\n", EPSILON*fabs(Eold/(Eplus-Eminus)));
although to be honest, I cheated and changed 1e-15 to EPSILON in this
example... which incidentally was from code I was working on just this
morning. It's estimating the fractional roundoff error in a finite
difference derivative calculation, which is being used to test a
gradient computation. So we use this to see whether our error is
likely to be overwhelmed by roundoff error, as a human examination
approach. In this case it isn't particularly important to use
EPSILON, since that's just a lower bound on the error, and we
typically have an order of magnitude or two more error than that.
--
David Roundy
double a, b;
...
if(a == b) /* WRONG */
use something like
#include <math.h>
if(fabs(a - b) <= epsilon * fabs(a))
Although everybody can make value for his own problem, a build-in
constant is helpful.
http://en.wikipedia.org/wiki/Machine_epsilon
A search on FLT_EPSILON (
http://www.google.com/search?q=FLT_EPSILON+filetype:c ) reveals it is
widely used.
Sanbo
Although this is still wrong in the same sense just a little less so.
It really depends on why you're checking for equality! To quote from
the link you cited:
"The precise value of epsilon may still have to be chosen with care:
its appropriate value may be quite small and related only to the
machine's floating-point precision, or it may be larger if the numbers
being compared are inherently less accurate or are the results of a
chain of calculations which compounds accuracy losses over several
steps. (Also, you may have to make the threshold a function of b, or
of both a and b.)"
The degree of fuzz needed is entirely dependent on how these numbers
are computed and what you're comparing for. Checking for exact
equality is great if you've got a stable iterative algorithm and you
want to iterate to convergence. Or you could do a fuzzy comparison if
you want less precision, but using FLT_EPSILON is probably only
correct in a minority of cases.
David