Reelle tal, f.eks. 1/7 ≈ float eller double
32 bit 64 bit ”By default, the x87 processors all use 80-bit double-extended precision internally“ wikipedia.org/wiki/X87
Reelle tal, f.eks. 1/7 eller double float 32 bit - - PowerPoint PPT Presentation
Reelle tal, f.eks. 1/7 eller double float 32 bit 64 bit By default, the x87 processors all use 80-bit double-extended precision internally wikipedia.org/wiki/X87 Algorithms using real numbers
Reelle tal, f.eks. 1/7 ≈ float eller double
32 bit 64 bit ”By default, the x87 processors all use 80-bit double-extended precision internally“ wikipedia.org/wiki/X87
Noter ch.4
double x = 0.0; if ( x == 0.0) << do something smart >> else << do something stupid >> assert ( x == 0.0 ); // C++ f( ) f( )
P.g.a. underlige compiler optimering og repræsentationer af reelle tal kan der faktisk ske ”something stupid”
if ( x == 0.0) << do something smart >> else << do something stupid >> assert ( x == 0.0 ); // C++ f( ) f( ) double x = 1.0;
P.g.a. underlige compiler optimering og repræsentationer af reelle tal kan der faktisk ske ”something stupid”
double d = 10; for (int i=0; i<10; i++) { System.out.println(d); d = d*d; }
double d = .10; for (int i=0; i<10; i++) { System.out.println(d); d = d*d; }
System.out.println( (1.0 / 49) * 49 ); }
hardware
characterized by
– a base ∈ \1 – a number of digits ∈ – a smallest exponent ∈ – largest exponent ∈
.
・ ・ ・
In decimal: 0.25 = (1+0/2+0/4) / 4 3.5 = (1+1/2+1/4) * 2
3.5 0.25
mantissa 110: binary number 1.10 Decimal number 1 + 1/2 + 0/4 = 1.5 mantissa 110 and exponent 1: binary number 11.0 Decimal number 1*2 + 1 + 0/2 = 3
7% 14% 62% 10% 8%
Limited precision a) X b) X c) X d) X e) X A number x in the number system F(2, 3,−2, 1) has the mantissa 110 and the exponent 1. What number is x in the usual decimal base 10 system? 1. 1.5 2. 3 3. 12 4. 11 5. I don’t know
A number x in the number system F(2, 3,−2, 1) has the mantissa 110 and the exponent 1. What number is x in the usual decimal base 10 system? 1. 1.5 2. 3 3. 12 4. 11 5. I don’t know Limited precision
A number x in the number system F(2, 3,−2, 1) has the mantissa 110 and the exponent 1. What number is x in the usual decimal base 10 system? 1. 1.5 2. 3 3. 12 4. 11 5. I don’t know Limited precision mantissa 110: binary number 1.10 Decimal number 1 + 1/2 + 0/4 = 1.5 mantissa 110 and exponent 1: binary number 11.0 Decimal number 1*2 + 1 + 0/2 = 3
not 1.573 0.1824 1.7554 1.573 0.1824 1.3906 1.573 0.1824 0.2869152 1.573 / 0.1824 8.6239035 . . .
number is needed
is machine numbers
⊕ etc.
e.g.
1.573 ⊕ 0.1824 1.573 0.1824 1.7554 1.755 1.573 ⊖ 0.1824 1.573 0.1824 1.3906 1.390 1.573 ⊗ 0.1824 1.573 0.1824 0.2869152 .2869 1.573 ⊘ 0.1824 1.573 / 0.1824 8.6239035 . . . 8.623
1.418 ⊕ 2937 ⊖ 2936 2938 ⊖ 2936 2.000 1.418 ⊕ 2937 ⊖ 2936 1.418 ⊕ 1.000 2.418 1.418 ⊗ 2001 ⊖ 2000 1.418 ⊗ 1.000 1.418 1.418 ⊗ 2001 ⊖ 1.418 ⊗ 2000 2837 ⊖ 2836 1.000
for machine arithmetic. Sometimes: ⊕ ⊖ ⊕ ⊖ ⊗ ⊖ ⊗ ⊖ ⊗
What is the result of computing (0.9996 ⊕ 0.9998) ⊘ 2 in the number system F(10, 4,−99, 99) using truncation? 1. 0.9995 2. 0.9996 3. 0.9997 4. 0.9998 5. I don’t know Rounding / truncation
What is the result of computing (0.9996 ⊕ 0.9998) ⊘ 2 in the number system F(10, 4,−99, 99) using truncation? 1. 0.9995 2. 0.9996 3. 0.9997 4. 0.9998 5. I don’t know Rounding / truncation (0.9996 ⊕ 0.9998) ⊘ 2 = fl(0.9996 + 0.9998) ⊘ 2 = fl(1.9994) ⊘ 2 = 1.999 ⊘ 2 = fl(1.999 / 2) = fl(0.9995) = 0.9995 Better expression for computing average: 0.9996 ⊕ (0.9998 ⊖ 0.9996) ⊘ 2 = 0.9997
3% 8% 8% 5% 77%
a) X b) X c) X d) X e) X
What is the result of computing (0.9996 ⊕ 0.9998) ⊘ 2 in the number system F(10, 4,−99, 99) using truncation? 1. 0.9995 2. 0.9996 3. 0.9997 4. 0.9998 5. I don’t know Rounding / truncation (0.9996 ⊕ 0.9998) ⊘ 2 = fl(0.9996 + 0.9998) ⊘ 2 = fl(1.9994) ⊘ 2 = 1.999 ⊘ 2 = fl(1.999 / 2) = fl(0.9995) = 0.9995 Better expression for computing average: 0.9996 ⊕ (0.9998 ⊖ 0.9996) ⊘ 2 = 0.9997
It is a fact that lim
→1
The algorithm computes an approximation to corresponding to 2 1024. What is the result of execution in the number system F(10,4,−99,99) with truncation, where fl() = 2.718?
Rounding / truncation int k = 10; m = 1; for (int i=0; i<k; i++) m = 2*m; s = 1 + 1/m; for (int i=0; i<k; i++) s = s*s; print s; m,s ∊ F(10,4,−99,99)
It is a fact that lim
→1
The algorithm computes an approximation to corresponding to 2 1024. What is the result of execution in the number system F(10,4,−99,99) with truncation, where fl() = 2.718?
Rounding / truncation 2 2 2 4 … 2 512 2 1024 1 1 1⊕1⊘1024 1⊕0.0009765 . ∗ . … ∗ . int k = 10; m = 1; for (int i=0; i<k; i++) m = 2*m; s = 1 + 1/m; for (int i=0; i<k; i++) s = s*s; print s; m,s ∊ F(10,4,−99,99)
2% 2% 86% 3% 7%
a) X b) X c) X d) X e) X
It is a fact that lim
→1
The algorithm computes an approximation to corresponding to 2 1024. What is the result of execution in the number system F(10,4,−99,99) with truncation, where fl() = 2.718?
Rounding / truncation 2 2 2 4 … 2 512 2 1024 1 1 1⊕1⊘1024 1⊕0.0009765 . ∗ . … ∗ . int k = 10; m = 1; for (int i=0; i<k; i++) m = 2*m; s = 1 + 1/m; for (int i=0; i<k; i++) s = s*s; print s; m,s ∊ F(10,4,−99,99)
approximately
– Single precision F(2, 24,−126, 127) – Double precision F(2, 53,−1022, 1023)
– closes the representational gap around zero with xtra numbers of the form 0. . . . ∗ 2 – has representations for ∞ and NaN (= Not a Number)
representable numbers, ex.
– overflow: 1/0 or 2∗ 2 has result ∞ – underflow: 1/∞ or 2/2 has result 0 – 0/0 or ∞ ∞ has result NaN
types float and double on most computers
independent of the underlying hardware.
– . . . . ∗ 2, where ∈ 0, 1 and 1022 1023 – ∞ and NaN (Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY , Double.NaN)
representable: literal 0.2 is represented as
– 0.2 0.00110011 . . .
1.1001100110011001100110011001100110011001100110011010 ・2
What is written?
Type double public strictfp void test() { double a = 1; for (int i = 0; i< 1000; i++) a *= 2; System.out.println(a); } double is approximately F(2,53,−1022,1023)
What is written?
Type double 2E1000 denotes 2 ∗ 10 2 1.0715086071862673E301 denotes 1.0715086071862673 ∗ 10 2 Double.POSITIVE_INFINITY denotes value larger than approximately 2 public strictfp void test() { double a = 1; for (int i = 0; i< 1000; i++) a *= 2; System.out.println(a); } double is approximately F(2,53,−1022,1023)
3% 28% 26% 2% 42%
a) X b) X c) X d) X e) X
What is written?
Type double 2E1000 denotes 2 ∗ 10 2 1.0715086071862673E301 denotes 1.0715086071862673 ∗ 10 2 Double.POSITIVE_INFINITY denotes value larger than approximately 2 public strictfp void test() { double a = 1; for (int i = 0; i< 1000; i++) a *= 2; System.out.println(a); } double is approximately F(2,53,−1022,1023)
n‐th Harmonic number Hn = 1/1 + 1/2 + 1/3 +∙∙∙+ 1/n = 1/i
n i = 1
1/n n 1 2 3 4 5 1/1 1/2 1/3 1/4 1/n
1/x dx = [ ln x ] = ln n – ln 1 = ln n
n 1 n 1
Hn – 1 Hn – 1/n ln n + 1/n Hn ln n + 1
1 1 1 2 1 3 ・ ・ ・ 1
1 1⊕ 1 2 ⊕ 1 3 ⊕・ ・ ・⊕ 1
for n ≥ 1000 since 7.069⊕ 1 1001 7.069⊕0.0009990 7.069
1 1⊕ 1 2 ⊕ 1 3 ⊕・ ・ ・⊕ 1
1 1⊕ 1 2 ⊕ 1 3 ⊕ 1 4 ⊕・ ・ ・⊕ 1
1 1⊕ 1 2 ⊕ 1 3 ⊕ 1 4 ⊕・ ・ ・⊕ 1
– compute ・ ・ ・
– ⊕ not associative
– Add terms in increasing order – Try to add terms of equal size only
We want to compute
approximation?
their sum
Summation
We want to compute
approximation?
their sum
Summation
16% 5% 4% 73% 2%
a) X b) X c) X d) X e) X
We want to compute
approximation?
their sum
Summation
→
for ′
is root of
→
is root of
public static double sqrt(double a) { double xnew = (a + 1)/2; double xold = xnew + 1; while (???) { xold = xnew; xnew = xold − (xold*xold − a)/2/xold; } return xold; }
What is proper stopping criteria for square root computation by Newton iteration?
Stopping criteria public static double sqrt(double a) { double xnew = (a + 1)/2; double xold = xnew + 1; while (???) { xold = xnew; xnew = xold − (xold*xold − a)/2/xold; } return xold; } (since for some (Since we are satisfied with a small absolute error | | ) (Since we are satisfied with a small relative error | | ) (since we cannot really trust any of the other criteria)
What is proper stopping criteria for square root computation by Newton iteration?
Stopping criteria public static double sqrt(double a) { double xnew = (a + 1)/2; double xold = xnew + 1; while (???) { xold = xnew; xnew = xold − (xold*xold − a)/2/xold; } return xold; } (since for some (Since we are satisfied with a small absolute error | | ) (Since we are satisfied with a small relative error | | ) (since we cannot really trust any of the other criteria)
3% 33% 26% 24% 14%
a) X b) X c) X d) X e) X
What is proper stopping criteria for square root computation by Newton iteration?
Stopping criteria public static double sqrt(double a) { double xnew = (a + 1)/2; double xold = xnew + 1; while (???) { xold = xnew; xnew = xold − (xold*xold − a)/2/xold; } return xold; } (since for some (Since we are satisfied with a small absolute error | | ) (Since we are satisfied with a small relative error | | ) (since we cannot really trust any of the other criteria)
voksende !
– Same as ¬ 0 while (xold – xnew > 0)
3 8 10 0
1 8 10 3
1
8 10 3
Using x 0 Gives convergence: x x ⋯ ⋯ x x
| |
never get closer than | | 3 ∗
with care!
– Compute lim
→ for
– Using limited precision arithmetic, the series may fail to converge – Naive stopping criteria may lead to looping forever!
– stop iteration when adjacent terms are sufficiently close – the meaning of “sufficiently close” depends on the concrete series – for monotone convergent series it may be ok to loop until strict monotonicity fails