< previous page page_531 next page >

Page 531
This allowed for the maximum precision possible.
Underflow and Overflow
In addition to representational errors, there are two other problems to watch out for in floating point arithmetic: underflow and 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:
0531-01.gif
This value cannot be represented in our scheme because an exponent of -13 is too small. Our minimum is -9. One way to resolve the 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 there is no logical recourse when it occurs. For example, the result of the calculation
0531-02.gif
cannot be stored, so what should we do? To be consistent with our response to underflow, we could set the result to 9999 * 109 (the maximum representable value in this case). Yet this seems intuitively wrong. The alternative is to stop with an error message.
C++ does not define what should happen in the case of overflow or underflow. Different implementations of C++ solve the problem in different ways. You might try to cause an overflow with your system and see what happens. Some systems print a run-time error message such as FLOATING POINT OVERFLOW. On other systems, you may get the largest number that can be represented.
We have been discussing problems with floating point numbers, but integer numbers also can overflow both negatively and positively. Most implementations of C++ ignore integer overflow. To see how your system handles the situation, you should try adding 1 to INT_MAX and -1 to INT_MIN. On most systems, adding 1 to INT_MAX sets the result to INT_MIN, a negative number.
Sometimes you can avoid overflow by carefully arranging computations. Suppose you would like to know how many different poker hands are possible. There are 52 cards in a poker deck and 5 cards in one hand. What we are looking for is the number of combinations of 52 cards taken 5 at a time. The standard mathematical formula for the number of combinations of n things taken r at a time is
0531-03.gif

 
< previous page page_531 next page >