Programming and Problem Solving with Java

(やまだぃちぅ) #1
12.5 F l o ating-Point Numbers | 595

+ – 4103

–– 3540

–– 6300

+ – 1476

2

6

0

0

Sign Exp
0.1032 +1032 10–^4 0.1032


  • 5.4060 –5406 10–^3 – 5.406

    • 0.003 –3000 10–^6 – 0.0030




476.0321 +4760 10–^1 476.0

NUMBER POWER OF TEN
NOTATION

CODED REPRESENTATION VALUE

Figure 12.9 Coding of Some Floating-point Numbers


the 321 cannot be represented in our system. (Some computers, including all JVMs, perform
roundingrather than simple truncation when discarding excess digits. Using our assumption
of four significant digits, rounding would store 476.0321 as 476.0 but would store 476.0823 as
476.1. We continue our discussion assuming simple truncation rather than rounding.)


Arithmetic with Floating-Point Numbers


When we use integer arithmetic, our results are exact. Floating-point arithmetic, however,
is seldom exact. To understand why, let’s add three floating-point numbers x,y, and zusing
our coding scheme.
First we add xto y, and then we add zto the result. Next, we perform the operations in
a different order, first adding yto z,and then adding xto that result. The associative law of
arithmetic says that the two answers should be the same—but are they? Let’s use the fol-
lowing values for x,y, and z:


x=  1324  103 y= 1325  103 z= 5424  100

Here is the result of adding zto the sum of xand y:

(x) –1324  103
(y) 1325  103
1  103  1000  100

(xy) 1000  100
(z) 5424  100
6424  100 ←(xy)z
Free download pdf