Floating point. Exercises
CS/SE 4X03
Ned Nedialkov McMaster University January 28, 2021
FP addition and multiplication are not associative.
• a+(b+c) may not be the same as (a+b)+c
• a∗(b∗c) may not be the same as (a∗b)∗c
Copyright © 2021 N. Nedialkov. All rights reserved. 2/1
Adding FP numbers
Example 1. 0.59 + 0.24 × 10−1 + 0.26 × 10−2 + 0.64 × 10−3 = 0.61724 Add in 2-digit (after “.”) arithmetic with rounding to nearest.
In decreasing magnitude:
0.59 + 0.24 × 10−1 = 0.614 0.61 + 0.26 × 10−2 = 0.6126 0.61 + 0.64 × 10−3 = 0.61064
error |0.61724 − 0.61| = 7.2 × 10−3 In increasing magnitude:
0.64 × 10−3 + 0.26 × 10−2 = 0.00324 0.32 × 10−2 + 0.24 × 10−1 = 0.0272
0.27 × 10−1 + 0.59 = 0.617 error |0.61724 − 0.62| = 2.7 × 10−3
→ 0.61 → 0.61 → 0.61
The error can be smaller (but not always) if added in increasing magnitude.
Copyright © 2021 N. Nedialkov. All rights reserved. 3/1
→ 0.32 × 10−2 → 0.27 × 10−1 → 0.62
Example 2. For what range of x is
ex − 1 ≈ 0.5 correct to 15 decimal digits?
2x
Fromex =1+x+x2 +x3 +x4 +···,
2 3! 4!
x x2 x3 x4
e −1=x+ 2 +3!+4!+··· ex−1 xx2 x3
2x =0.5+4+2·3!+2·4!+··· ex−1 xx2 x3
2x −0.5=4+2·3!+2·4! If |x| ≤ 4 × 10−15,
ex −1 |x| −15 2x −0.5 4 ≤10
The remaining terms are very small.
Copyright © 2021 N. Nedialkov. All rights reserved. 4/1
Example 3 (p. 64, exercise 12). Given x ∈ [−π/2, π/2] and a value for cos x, compute
sin x = ±1 − cos2 x.
When x ≈ 0, cos2 x ≈ 1 and cancellations can occur in 1 − cos2 x.
Compute using
x − x 3 + x 5 6 120
| x | ≤ ε
ε < x ≤ π/2 −π/2 ≤ x < −ε,
sin(x) ≈ 1 − cos2(x) −1 − cos2(x)
where take for example ε = 10−6.
Copyright © 2021 N. Nedialkov. All rights reserved. 5/1
Example 4 (p. 65, exercise 29). Consider solving x2 − 109x + 1 = 0.
In double precision, b2 − 4ac = 108 − 4 evaluates to 108. Then using the standard formula,
√ x1,2=−b+ b2−4ac,
2a the roots in double precision are
x1 =0, x2 =−109. Using x1x2 = c/a, we can we can find
x1 = c/(ax1) = −10−9. Try also Quadratic Equation Calculator
Copyright © 2021 N. Nedialkov. All rights reserved. 6/1
Example 5 (p. 64, exercise 23). Let x0 > −1 and consider xn+1 = 2n+1 √1 + 2−nxn − 1 for n ≥ 0.
This sequence converges to ln(x0 + 1). Rewrite this iteration as
√
xn+1 =2n+11+2−nxn −1√1+2−nxn +1
1+2−nxn +1
= 2n+1 √ 2−nxn 1+2−nxn +1
= 2√
xn 1+2−nxn +1
Copyright © 2021 N. Nedialkov. All rights reserved. 7/1
The following Matlab code
clear all; close all;
x0 = -0.5; n = 100;
x(1) = x0;
for n =0:n
x(n+2) = 2^(n+1)*(sqrt(1+2^(-n)*x(n+1))-1);
end
semilogy([1:length(x)], abs(x-log(x0+1)))
for n =0:n
x(n+2) = 2*x(n+1)/(sqrt(1+2^(-n)*x(n+1))+1);
end
hold on
semilogy([1:length(x)], abs(x-log(x0+1)));
legend(’error in $x_{n+1} = 2^{n+1}\left(\sqrt{1+2^{-n}x_n}-1\right)$’
,…
’error in $x_{n+1} = 2 {x_n}/({\sqrt{1+2^{-n}x_n}+1})$’,… ’interpreter’,’latex’, ’Location’, ’NorthWest’)
xlabel(’n’)
ylabel(’error’)
set(gca, ’FontSize’, 12);
print(’-depsc2’, ’p64e23.eps’)
produces
Copyright © 2021 N. Nedialkov. All rights reserved. 8/1
15
10
10
10
5
10
0
10
-5
10
-10
10
-15
10
10
-20
0 20 40 60 80 100 120
n
Copyright © 2021 N. Nedialkov. All rights reserved. 9/1
error
Example 6. For what values of x the expression
ex − sin(x) − cos(x) can have cancellations and how to avoid them?
Whenx≈0,ex ≈1,sin(x)≈0,cos(x)≈1. When x≈0
x x2x3 e ≈1+x+2+6
sin(x) ≈ x − x3 6
cos(x) ≈ 1 − x2 2
ex − sin(x) − cos(x) ≈ x2 + x3 3
For small x, use x2 + x3 3
Copyright © 2021 N. Nedialkov. All rights reserved. 10/1