Mail Archives: djgpp/1997/05/01/06:45:20
George Foot wrote:
> There is still
> perhaps the possibility that current could go from (target-SPEED/2-delta)
> to (target+SPEED/2+delta) where delta is a really small number, and then
This delta is called the ``machine epsilon''. Specifically, all
comparisons between floats and doubles should be given relative
tolerance of that epsilon. Therefore, where for ints you would say
just this:
int i, j;
int i_equals_j = (i == j);
for doubles you should say instead this:
double a, b;
double max_abs = max (fabs (a), fabs (b));
int a_equals_b = (a - b < max_abs * eps && b - a < max_abs * eps);
Note that eps is the *relative* tolerance; hence the actual tolerance
is computed by multiplying it by the maximum absolute value of the two
comparees.
The header <float.h> defines a macro DBL_EPSILON which should be used
as the value of `eps' in the above snippet. Two other macros,
FLT_EPSILON and LDBL_EPSILON give values for floats and long doubles,
respectfully.
- Raw text -