Programming and Problem Solving with Java

(やまだぃちぅ) #1
12.6 Decimal Format Type | 603

Practical Implications of Limited Precision


A discussion of representational, overflow, underflow, and cancellation errors may seem purely ac-
ademic. In fact, these errors have serious practical implications in many problems. We close this
offer three examples illustrating how limited precision can have costly or even disastrous effects.
During the Mercury space program, several of the spacecraft splashed down a considerable
distance from their computed landing points. Each mishap delayed the recovery of the space-
craft and the astronaut, putting both in some danger. Eventually, the problem was traced to an
imprecise representation of the Earth’s rotation period in the code that calculated the landing
point.
As part of the construction of a hydroelectric dam, a long set of high-tension cables had to
be constructed to link the dam to the nearest power distribution point. The cables were to be
several miles long, and each one was to be a continuous unit. (Because of the high power
output from the dam, shorter cables couldn’t be spliced together.) The cables were constructed
at great expense and strung between the two points. It turned out that they were too short,
however, so another set had to be manufactured. The problem was traced to errors of precision
in calculating the length of the catenary curve (the curve that a cable forms when hanging be-
tween two points).
An audit of a bank turned up a mysterious account with a large amount of money in it. The
account was traced to an unscrupulous programmer who had used limited precision to his ad-
vantage. The bank computed interest on its accounts to a precision of a tenth of a cent. The
tenths of cents were not added to the customers’ accounts, so the programmer had the extra
tenths for all the accounts summed and deposited into an account in his name. Because the
bank had thousands of accounts, these tiny amounts added up to a large amount of money.
And because the rest of the bank’s applications did not use as much precision in their calcula-
tions, the scheme went undetected for many months.
The moral of this discussion is twofold: (1) The results of floating-point calculations are of-
ten imprecise, and these errors can have serious consequences; and (2) if you are working with
extremely large numbers or extremely small numbers, you need more information than this
book provides and should consult a numerical analysis text.

2.Declare a variable of type DecimalFormatfor each number format we wish to use.
3.For each format, create an object of class DecimalFormatthat contains the pattern.
4.Format a number using the formatmethod of one of the DecimalFormatobjects.
Let’s examine each of these steps in turn. You are already familiar with writing importdec-
larations, so all you need to do for the first step is to remember to put the declaration at the

Free download pdf