HP-UX Floating-Point Guide
84 Chapter 3
Factors that Affect the Results of Floating-Point Computations
Floating-Point Coding Practices that Affect Application Results
The following sections describe common floating-point programming
mistakes that can lead to incorrect application results:
• Testing floating-point values for equality
• Taking the difference of similar values
• Adding values with very different magnitudes
• Unintentional underflow
• Truncation to an integer value
• Ill-conditioned computations
The first mistake produces results that are simply wrong. The others are
more insidious: they usually cause the result of an operation to lose a
substantial amount of precision.
NOTE The illustrations in these sections use decimal representations, rather
than binary or hexadecimal ones, so as to correspond more closely to the
way most users think about floating-point values. These examples
illustrate general principles; they cannot necessarily be reproduced
using IEEE-754 arithmetic.
Testing Floating-Point Values for Equality
Think very carefully before you test two floating-point values for
equality. Because of the inherent inexactness of floating-point
representations and because of the many sources of rounding
inaccuracies in a floating-point computation, values that should be equal
from a purely algebraic perspective in fact rarely will be.
For example, in IEEE double-precision arithmetic 1.2 − 0.1 is not exactly
equal to 1.1. Consider the following example.