HP-UX Floating-Point Guide

90 Chapter 3
Factors that Affect the Results of Floating-Point Computations
Floating-Point Coding Practices that Affect Application Results
Sample Program: diffmag1.f
PROGRAM DIFFMAG1
REAL X
INTEGER I
X = 0.01
DO 10 I = 1, 1000
X = X + 0.01
10 CONTINUE
PRINT *, 'X is', X
DO 20 I = 1, 1000
X = X - 0.01
20 CONTINUE
PRINT *, 'X is', X
END
The result is not exact. Instead of 10.01 and 0.01, you are likely to get
results similar to the following:
X is 10.01013
X is 9.99994E-03
The following example is even simpler. At higher magnitudes, adding 1
to a value has no effect at all.
Sample Program: diffmag2.f
REAL BIG, SMALL, SUM, DIFF
INTEGER I
SMALL = 1.0
DO I = 1, 10
BIG = 10.0**I
SUM = BIG + SMALL
DIFF = ABS(BIG - SUM)
PRINT 100, SUM, DIFF
END DO
100 FORMAT(F14.1, F8.3)
END
The addition of 1 stops affecting the result when the value reaches about
10
8
:
11.0 1.000
101.0 1.000
1001.0 1.000
10001.0 1.000
100001.0 1.000
1000001.0 1.000
10000001.0 1.000
100000000.0 .000
1000000000.0 .000
10000000000.0 .000