MATH1070 2 Error and Computer Arithmetic PDF
MATH1070 2 Error and Computer Arithmetic PDF
MATH1070 2 Error and Computer Arithmetic PDF
Math 1070
Math 1070
Math 1070
integer format
floating-point format
(4.1)
where
= +1 or 1 is the sign
e is an integer, the exponent
1 x < 10, the significand or mantissa
Example (
2
124.62 = (1.2462
| {z }) 10
Math 1070
size of e
Example
Suppose we limit
1
number of digits in x to 4
99 e 99
Math 1070
(4.2)
where
= +1 or 1 is the sign
e is an integer, the exponent
x is a binary fraction satisfying
(1)2 x < (10)2
For example, if
x = (11011.0111)2
then = +1, e = 4 = (100)2 and x = (1.10110111)2
2. Error and Computer Arithmetic
Math 1070
Math 1070
b2 b 3 b 9
| {z }
E=e+127
b b b
|10 11{z 32}
x
b b b
|2 3 {z 12}
E=e+1023
Math 1070
> 2. Error and Computer Arithmetic > 2.1.1 Accuracy of floating-point representation
Epsilon machine
How accurate can a number be stored in the floating point representation?
How can this be measured?
(1) Machine epsilon
Machine epsilon
For any format, the machine epsilon is the difference between 1 and the next
larger number that can be stored in that format.
In single precision IEEE, the next larger binary number is
1.0000000000000000000000 |{z}
1
a23
Math 1070
> 2. Error and Computer Arithmetic > 2.1.1 Accuracy of floating-point representation
Math 1070
> 2. Error and Computer Arithmetic > 2.1.1 Accuracy of floating-point representation
Math 1070
> 2. Error and Computer Arithmetic > 2.1.2 Rounding and chopping
Math 1070
> 2. Error and Computer Arithmetic > 2.1.2 Rounding and chopping
(4.3)
(4.4)
2n 2n
(4.5)
Characteristics of chopping:
1 the worst possible error is twice as large as when rounding is used
2 the sign of the error x f l(x) is the same as the sign of x
The worst of the two: no possibility of cancellation of errors.
Characteristics of rounding:
1 the worst possible error is only half as large as when chopping is used
2 More important: the error x f l(x) is negative for only half the
cases, which leads to better error propagation behavior
2. Error and Computer Arithmetic
Math 1070
> 2. Error and Computer Arithmetic > 2.1.2 Rounding and chopping
223 0
(4.6)
224 224
(4.7)
standard rounding:
252 0
(4.8)
253 253
(4.9)
rounding:
Math 1070
> 2. Error and Computer Arithmetic >Conseq. for programming of floating-point arithm.
Numbers that have finite decimal expressions may have infinite binary
expansions. For example
(0.1)10 = (0.000110011001100110011 . . .)2
Hence (0.1)10 cannot be represented exactly in binary floating-point
arithmetic.
Possible problems:
Run into infinite loops
pay attention to the language used:
Fortran and C have both single and double precision, specify
double precision constants correctly
MATLAB does all computations in double precision
Math 1070
> 2. Error and Computer Arithmetic > 2.2 Errors: Definitions, Sources and Examples
Definition
The error in a computed quantity is defined as
Error(xA ) = xT - xA
where xT =true value, xA =approximate value. This is called also
absolute error.
The relative error Rel(xA ) is a measure off error related to the
size of the true value
error = xT xA
Rel(xA ) =
xT
true value
For example, for the approximation
22
=
7
we have xT = = 3.14159265 . . . and xA = 22
7 = 3.1428571
22
22
=
= 0.00126
Error
7
7
22
22
7
Rel
=
= 0.00042
7
Math 1070
> 2. Error and Computer Arithmetic > 2.2 Errors: Definitions, Sources and Examples
Math 1070
> 2. Error and Computer Arithmetic > 2.2 Errors: Definitions, Sources and Examples
am am+1 am+2
0 bm+1 bm+2
xA = 0.222, xT =
2
9
xT = 0. 2 2 2 2 2 2
3
|xT xA | = 0. 0 0 0 2 2 2
Math 1070
> 2. Error and Computer Arithmetic > 2.2 Errors: Definitions, Sources and Examples
Example
1
xA = 23.496, xT = 23.494:
xT = 2 3. 4 9 6
4
|xT xA | = 0 0. 0 0 1 9 9
xA = 0.02138, xT = 0.02144:
xT = 0. 0 2 1 4 4
|xT xA | = 0. 0 0 0 0 6
xA =
22
7
= 3.1428571 . . . , xT = = 3.14159265 . . . :
xT = 3. 1 4 1 5 9 2 6 5
3
|xT xA | = 0. 0 0 1 2 6 4 4 8
Math 1070
Modeling errors
Blunders and mistakes
Physical measurement errors
Machine representation and arithmetic errors
Mathematical approximation errors
Consequences of errors
(F1) Loss-of-significance errors
(F2) Noise in function evaluation
(F3) Underflow and overflow erros
Math 1070
N0 , k 0
Math 1070
|| 0.000003
Due to the error in data, the calculations will contain the effect
of this observational error. Numerical analysis cannot remove the
error in the data, but it can
look at its propagated effect in a calculation and
suggest the best form for a calculation that will minimize the
propagated effect of errors in data
Math 1070
Math 1070
ex 1 x2 +
x4 x6 x8
+
2!
3!
4!
+
dx
2!
3!
4!
0
with the truncation error being evaluated by (3.10)
Math 1070
Example
Consider the evaluation of
f (x) = x[ x + 1 x]
Math 1070
What happens?
For x = 100:
where
100 = 10.0000
| {z },
exact
101 = 10.04999
| {z }
rounded
has a loss-of-significance
accuracy in x + 1 = 101 were
canceled
by subtraction of the
Math 1070
x+1 x
Math 1070
Example
Consider the evaluation of
1 cos(x)
f (x) =
x2
for a sequence approaching 0.
x0
0.1
0.01
0.001
0.0001
0.00001
Computed f (x)
0.4995834700
0.4999960000
0.5000000000
0.5000000000
0.0
True f(x)
0.4995834722
0.4999958333
0.4999999583
0.4999999996
0.5000000000
Math 1070
(= 0.999950000416665)
has nine significant digits of accuracy, being off in the tenth digit by
two units. Next
1 cos(0.01) = 0.0000499996
(= 4.999958333495869e 05)
which has only five significant digits, with four digits being lost in the
subtraction.
To avoid the loss of significant digits, due to the subtraction of nearly
equal quantities, we use the Taylor approximation (3.7) for cos(x)
about x = 0:
cos(x) = 1
R6 (x) =
x2 x4 x6
+
+ R6 (x)
2! 4!
6!
x8
cos(),
8!
Hence
x2 x4 x6
1
+
+ R6 (x)
f (x) = 2 1 1
x
2!
4!
6!
1
x2 x4 x6
=
+
cos()
2!
4!
6!
8!
giving f (0) = 12 . For |x| 0.1
6
6
x
cos() 10 = 2.5 1011
8!
8!
Therefore, with this accuracy
f (x)
1
x2 x4
+ ,
2!
4!
6!
Math 1070
Remark
When two nearly equal quantities are subtracted, leading significant
digits will be lost.
In the previous two examples, this was easy to recognize and we found
ways to avoid the loss of significance.
More often, the loss of significance is subtle and difficult to detect, as
in calculating sums (for example, in approximating a function f (x) by
a Taylor polynomial). If the value of the sum is relatively small
compared to the terms being summed, then there are probably some
significant digits of accuracy being lost in the summation process.
Math 1070
Example
Consider using the Taylor series approximation for ex to evaluate e5 :
(5) (5)2
(5)3
(5)4
e5 = 1 +
+
+
+
+ ...
1!
2!
3!
4!
Degree
Term
Sum
Degree
0
1.000
1.000
13
1
-5.000
-4.000
14
2
12.50
8.500
15
3
-20.83
-12.33
16
4
26.04
13.71
17
5
-26.04
-12.33
18
6
21.70
9.370
19
7
-15.50
-6.130
20
8
9.688
3.558
21
9
-5.382
-1.824
22
10
2.691
0.8670
23
11
-1.223 -0.3560
24
12
0.5097 0.1537
25
Table. Calculation of e5 = 0.006738 using
2. Error and Computer Arithmetic
Term
Sum
-0.1960
-0.04230
0.7001E-1
0.02771
-0.2334E-1 0.004370
0.7293E-2
0.01166
-0.2145E-2 0.009518
0.5958E-3
0.01011
-0.1568E-3 0.009957
0.3920E-4
0.009996
-0.9333E-5 0.009987
0.2121E-5
0.009989
-0.4611 E-6 0.009989
0.9607 E-7 0.009989
-0.1921 E-7 0.009989
four-digit decimal arithmetic
Math 1070
1
1
=
5
e
series for e5
Math 1070
> 2. Error and Computer Arithmetic > 2.2.3 Noise in function evaluation
Math 1070
> 2. Error and Computer Arithmetic > 2.2.3 Noise in function evaluation
Math 1070
> 2. Error and Computer Arithmetic > 2.2.3 Noise in function evaluation
Math 1070
> 2. Error and Computer Arithmetic > 2.2.4 Underflow and overflow errors
Math 1070
> 2. Error and Computer Arithmetic > 2.2.4 Underflow and overflow errors
Math 1070
> 2. Error and Computer Arithmetic > 2.2.4 Underflow and overflow errors
q
Math 1070
When doing calculations with numbers that contain an error, the result
will be effected by these errors.
If xA , yA denote numbers used in a calculation, corresponding to
xT , yT , we wish to bound the propagated error
E = (xT yT ) (xA yA )
where denotes: + , , , .
The first technique used to bound E is interval arithmetic.
Suppose we know bounds on xT xA and yT yA . Using these bound
and xA yA , we look for an interval guaranteed to contain xT yT .
Math 1070
Example
|yA yT | 0.0005
3.135 xT 3.145
2.6505 yT 2.6515
(4.10)
(4.11)
(4.12)
Math 1070
Example
For division ( : 00 )
xA
3.14
=
= 1.184459
yA
2.651
(4.13)
2.6515
yT
2.6505
dividing and rounding to 7 digits:
1.182350
xT
1.186569
yT
The bound on E
0.002109
2. Error and Computer Arithmetic
xT
1.184459 0.002110
yT
Math 1070
xT yT xA yA
xT yT
If xT = xA + , yT = yA + , then
xT yT xA yA
xT yT (xT )(yT )
=
xT yT
xT yT
xT + yT
=
=
+
xT yT
xT
yT
xT yT
= Rel(xA ) + Rel(yA ) Rel(xA )Rel(yA )
Rel(xA yA ) =
Math 1070
xA
yA
compared to xyTT is
xT
xA
y
= T xT
Rel
yA
yT
xA
yA
If xT = xA + , yT = yA + , then
Rel
xA
yA
=
xT
yT
xA
yA
xT
yT
xT + yT
x y
= T T
xT (yT )
1 xT
Rel(xA ) Rel(yA )
1 Rel(yA )
If Rel(yA ) 1, then
Rel
2. Error and Computer Arithmetic
xA
yA
Rel(xA ) Rel(yA )
Math 1070
xA = 13
xT =
yT = 168, yA = 12.961
1
Rel(xA ) = 0, Rel(yA ) = 0.0000371
Error(xA yA ) = 0.0004814
Rel(xA yA ) = 0.0125
xT = xA = 3.1416
yT = 22
7 , yA = 3.1429
x x =
7.35 106 Rel(xA ) = 2.34 106 ,
T
A
2
Rel(xA yA ) = 0.28
2. Error and Computer Arithmetic
Math 1070
error in computing xA yA
xA
yA = f l(xA yA ) = (1 + )(xA yA )
where as in (4.4)-(4.5), we get
xA yA xA
yA = (xA yA )
xA yA xA
yA
=
xA yA
Hence the process of rounding or chopping introduces a relatively small
new error into xA
yA as compared with xA yA .
2. Error and Computer Arithmetic
Math 1070
> 2. Error and Computer Arithmetic > 2.3.1 Propagated error in function evaluation
(4.14)
and
Rel(f (xA ))
f 0 (xT )
f 0 (xT )
(xT xA ) =
xT Rel(xA )
f (xT )
f (xT )
(4.15)
Math 1070
> 2. Error and Computer Arithmetic > 2.3.1 Propagated error in function evaluation
|| 0.0012
xA = 8.3143,
1
R,
|xT xA | 0.0012
1
1
R 8.3143
we have
(4.14)
1
x2A
0.0012 = 0.000144
Math 1070
> 2. Error and Computer Arithmetic > 2.3.1 Propagated error in function evaluation
K = 104
Rel(bxA ) = 103
independent of how bxA is actually computed.
2. Error and Computer Arithmetic
Math 1070
n
X
aj ,
j=1
Math 1070
then summate
decimal fraction
Math 1070
n
10
25
50
100
200
500
1000
True
2.929
3.816
4.499
5.187
5.878
6.793
7.486
SL
2.929
3.813
4.491
5.170
5.841
6.692
7.284
Error
0.001
0.003
0.008
0.017
0.037
0.101
0.202
LS
2.927
3.806
4.479
5.142
5.786
6.569
7.069
Error
0.002
0.010
0.020
0.045
0.092
0.224
0.417
True
2.929
3.816
4.499
5.187
5.878
6.793
7.486
SL
2.929
3.816
4.500
5.187
5.878
6.794
7.486
Error
0
0
-0.001
0
0
0.001
0
LS
2.929
3.817
4.498
5.187
5.876
6.783
7.449
Error
0
-0.001
0.001
0
0.002
0.010
0.037
Math 1070
> 2. Error and Computer Arithmetic > 2.4.1 Rounding versus chopping
(4.17)
(1) Assuming that the previous example used rounding with four-digit
decimal machine, we know that all j satisfy
0.005 j 0.005
(4.18)
Treating rounding errors as being random, then the positive and negative
values of the j s will tend to cancel and the sum T will be nearly zero.
Moreover, from probability theory:
Math 1070
> 2. Error and Computer Arithmetic > 2.4.1 Rounding versus chopping
Math 1070
> 2. Error and Computer Arithmetic > 2.4.1 Rounding versus chopping
n
X
1
j=1
True
2.92896825
4.49920534
5.18737752
6.79282343
7.48547086
1
j
Rounding error
-1.76E-7
7.00E-7
-4.12E-7
-1.32E-6
8.88E-8
Chopping Error
3.01E-7
3.56E-6
6.26E-6
3.59E-5
7.35E-5
Math 1070
(4.19)
(4.20)
Math 1070
Math 1070
j
10
20
30
40
50
60
70
80
90
100
x
1
2
3
4
5
6
7
8
9
10
Math 1070
> 2. Error and Computer Arithmetic > 2.4.3 Calculation of inner products
n
X
aj bj
(4.21)
j=1
Math 1070
> 2. Error and Computer Arithmetic > 2.4.3 Calculation of inner products
2
3
4
Math 1070