Graphics Reference
In-Depth Information
suffers from cancellation errors when x is near zero. By rewriting the function as
sin 2 ( x )
1
2 sin 2 ( x /2),
f ( x )
=
or
f ( x )
=
+
cos x
this problem is avoided. Similarly, the function
= x
x
g ( x )
+
1
has a cancellation error for large x . This error is removed by rewriting the function as
1
g ( x )
=
x
+ x .
+
1
Many practical examples of avoiding errors in finite-precision calculations are
provided in [Acton96].
11.3 Robust Floating-point Usage
Despite some apparent shortcomings, floating-point arithmetic often remains the
most convenient number representation for real-time applications such as collision
detection. With some care, the floating-point use in these applications frequently can
be made robust. The most important tool for accomplishing this task is the use of
tolerances. The following sections look at how tolerances are used with floating-point
numbers and, as examples, discusses how simple intersection tests of lines against
planes and triangles are made robust using tolerances. Another important device
is to ensure consistency between related calculations by properly sharing common
subexpressions between the calculations. This approach is again exemplified in terms
of the intersection between lines and triangles.
11.3.1 Tolerance Comparisons for Floating-point Values
Working with floating-point arithmetic, it is important to be aware of the fact that
direct comparisons of floating-point values are almost always the wrong thing to
do, due to the accumulation of rounding and cancellation errors. For example, a test
such as
if (x == 0.0f) ...
 
Search WWH ::




Custom Search