12.5 F l o ating-Point Numbers | 599
Chapter 4, we wrote an expression that compares two floating-point variables rand sfor near
equality using the floating-point absolute-value method Math.abs:
Math.abs(r – s) <0.00001
From our discussion of model numbers, you now can recognize that the constant 0.00001 in
this expression represents a maximum absolute error. We can generalize this expression as
Math.abs(r – s) < ERROR_TERM
where ERROR_TERMis a value that must be determined for each programming problem.
What if we want to compare floating-point numbers with a relative error measure? We
must multiply the error term by the value in the problem to which the error is relative. For
example, if we want to test whether rand sare “equal” within 0.05% of s, we write the fol-
lowing expression:
Math.abs(r – s) < 0.0005* s
Keep in mind that the choice of the acceptable error and the determination of whether
it should be absolute or relative depends on the particular problem at hand. The error terms
we have shown in our example expressions are completely arbitrary and may not be ap-
propriate for most problems. In solving a problem that involves the comparison of floating-
point numbers, you typically want to keep the error term as small as possible. Sometimes
the choice is specified in the problem description or is reasonably obvious. Some cases re-
quire careful analysis of both the mathematics of the problem and the representational lim-
its of the particular computer. Such analyses fall within the domain of a branch of
mathematics called numerical analysisand are beyond the scope of this text.
Underflow and Overflow In addition to representational errors, we must watch out for two other
problems in floating-point arithmetic: underflowand overflow.
Underflow is the condition that arises when the value of a calculation is too small to be
represented. Going back to our decimal representation, let’s look at a calculation involving
small numbers:
We cannot represent this value in our scheme because the exponent 13 is too small; our
minimum is 9. One way to resolve this problem is to set the result of the calculation to 0.0.
Obviously, any answer depending on this calculation will not be exact.
Overflow is a more serious problem because we have no logical recourse available when
it occurs. For example, the result of the calculation