Q:

Detect zero after std::remainder() call on doubles

I have this code:
double a = remainder(92.66, 0.01);
double b = remainder(92.87, 0.01);

And it turns out that a = -5.33948e-15and b = -2.61423e-15
The answer here is clearly zero, and if I multiplied both numbers by 100 and did integer modulus it would be. But I'd like to be able to do this using doubles. The problem is that remainder is returning a number that is bigger than DBL_EPSILON - so what is the correct(best approximation) way to determine whether a or b is ~zero?

A:

DBL_EPSILON is the smallest quantity that can be added to 1.0. If your number is larger than 1.0, the epsilon for that number will be proportionately larger as well. Simply multiply DBL_EPSILON by the number to get a threshold.
double a = remainder(92.66, 0.01);
if (a <= DBL_EPSILON * 92.66)
    a = 0.0;

