Graphics Reference
In-Depth Information
is likely to fail due to accumulated errors in x , making it perhaps very close to but
never quite equal to zero. A better approach in this case is to use a tolerance and
consider x zero when within some small range of values centered on zero. This is
done by writing the test as
if (Abs(x) <= epsilon) ...
where epsilon is a small value chosen to match the desired tolerance. This epsilon could
be as small as, say, 10 6 , but could also be upward of 0.1 or larger, all depending on
the range for x and the computations performed. Comparing x against an arbitrary
value y turns the test into its generalized form:
if (Abs(x - y) <= epsilon) ... // Absolute tolerance comparison
This described use of an epsilon tolerance value to compare two floating-point
values for equality is referred to as absolute tolerance , as the epsilon value is fixed. The
drawback with an absolute tolerance test is that it is difficult to find relevant epsilon
values. The epsilon value depends on the range of values for the input data and on the
floating-point format used. It is not possible to pick one epsilon value for the entire
range of floating-point numbers. For very small (nonequal) values of x and y their
difference may always be smaller, and for large values larger, than the epsilon value.
Another way of looking at it is that as the tested numbers grow larger the absolute
test requires that more and more digits agree. When the numbers are sufficiently
larger than the fixed epsilon, the test will always fail unless the numbers are exactly
equal, which is usually not intended. An absolute tolerance should therefore only be
used when the orders of magnitude of the numbers are known in advance and the
tolerance value can be set accordingly.
An alternative solution is to use a relative tolerance . Here, the basic idea is to divide
the one number by the other and see how close the result is to 1. Assuming
|
x
|≤|
y
|
,
the test becomes
if (Abs(x/y-1.0f) <= epsilon) ...
This expression can be written as
if (Abs((x - y) / y) <= epsilon) ...
To avoid the costly division operation and to guard against division-by-zero errors,
multiplying by Abs(y) on both sides of the latter expression simplifies it to
if (Abs(x - y) <= epsilon * Abs(y)) ...
 
Search WWH ::




Custom Search