When to use DBL_EPSILON/epsilon

monkeyking picture monkeyking · Jun 24, 2010 · Viewed 12.3k times · Source

The DBL_EPSILON/std::numeric_limits::epsilon will give me the smallest value that will make a difference when adding with one.

I'm having trouble understanding how to apply this knowledge into something useful.

The epsilon is much larger than the smallest value the computer can handle, so It would seem like a correct assumption that its safe to use smaller values than epsilon?

Should the ratio between the values I'm working with be smaller than 1/epsilon ?

Answer

AProgrammer picture AProgrammer · Jun 24, 2010

The definition of DBL_EPSILON isn't that. It is the difference between the next representable number after 1 and 1 (your definition assumes that the rounding mode is set to "toward 0" or "toward minus infinity", that's not always true).

It's something useful if you know enough about numerical analysis. But I fear this place is not the best one to learn about that. As an example, you could use it in building a comparison function which would tell if two floating point numbers are approximatively equal like this

bool approximatively_equal(double x, double y, int ulp)
{
   return fabs(x-y) <= ulp*DBL_EPSILON*max(fabs(x), fabs(y));
}

(but without knowing how to determine ulp, you'll be lost; and this function has probably problems if intermediate results are denormals; fp computation is complicated to make robust)