ERRORS and COMPUTER ARITHMETIC Types of Error in Numerical Calculations Initial Data Errors: from experiment, modeling, computer representation; problem dependent but need to know at beginning of calculation. Truncation Error: from stopping algorithm with infinite number of steps; algorithm dependent, but need to be aware of for algorithm design. Roundoff error: from finite representation of numbers in computer during arithmetic computations; need to be aware of for algorithm design and interpretation of results. Measures of Error: for exact x and approximation x Absolute error e = x x. Relative error r = (x x )/x. 1
ERRORS and COMPUTER NUMBERS Computer Numbers, Roundoff Error Floating Point Number Representation: any real number y can be put in floating point form y = ±.d 1 d 2... d k... b ±n for base b, with 0 < d 1 < b, 0 d i < b, i > 1. E.g. (.1) 10 = +(.1100) 2 2 3. Conversion from decimal to binary? Integer part: repeatedly divide by 2, saving remainder digits d i. Fraction: repeatedly multiply by 2, saving remainder digits d i. E.g. (4.1) 10 = +(.1000001100) 2 2 3. E.g. (3.1) 10 = +(.110001100) 2 2 2. Computer numbers: computers use y fl(y) = ±.d 1d 2... d k b ±e 1e 2...e m where b, k, m are machine dependent, 0 e i < b. Digits d 1... d k are mantissa, e 1e 2... e m exponent. Note: real computer arithmetic uses only a finite subset of (, ). 2
COMPUTER NUMBERS CONTINUED IEEE Standard for b = 2 number representation: single precision (SP) with ±, (k, m) = (23, 8); double precision (DP) with ±, (k, m) = (52, 11); long double with ±, (k, m) = (64, 15). Normalized IEEE form: fl(y) = ±1.b 1 b 2... b k 2 p. E.g. for DP, 1 2 = +1.000... 000 2 0, with 52 zeros; next DP number is (51 zeros) (1 + 2 52 ) 2 = +1.000... 001 2 0. Special representations for ±0, ± and NAN (e.g. 0 0 ), using special values for p (= -1023 or 1024 for DP). Overflow occurs if p > largest +e 1 e 2... e m ; with DP p 1023, note 2 1023 9 10 307. Underflow occurs if p < smallest e 1 e 2... e m ; with DP p 1022, note 2 1022 2 10 308, but p = 1023 allows subnormal numbers 2 1074. 3
COMPUTER NUMBERS CONTINUED Machine Epsilon: macheps or unit roundoff or ɛ mach is the smallest number ɛ mach for which fl(1 + ɛ mach ) > 1. IEEE SP has ɛ mach = 2 23 10 7 ; IEEE DP has ɛ mach = 2 52 2 10 16 (MATLAB). Matlab Checks: format short g, disp([1+2^(-52) 1+2^(-53)]) 1 1 disp([1+2^(-52)-1 1+2^(-53)-1]) 2.2204e-16 0 disp(sprintf( %20.17f,[1+2^(-52) 1+2^(-53)])) 1.00000000000000022 1.00000000000000000 disp(sprintf( %20.17f,[1+2^(-52)-1 1+2^(-53)-1])) 0.00000000000000022 0.00000000000000000 4
COMPUTER NUMBERS CONTINUED Rounding: if y does not have exact f l(y) representation, we need a method for choosing last digit(s). E.g. SP fl(.1) = +1.1001100110011001100110? 2 4. chopping discards all b i, i > k, so that SP fl(.1) = +1.10011001100110011001100 2 4 ; DP fl(.1) = +1.100110011001... 10011001 2 4, DP fl(4.1) = +1.000001100110... 01100110 2 2, DP fl(3.1) = +1.100011001100... 11001100 2 1. rounding (to nearest) adds 1 to b k if b k+1 = 1; so SP fl(.1) = +1.10011001100110011001101 2 4. DP fl(.1) = +1.100110011001... 10011010 2 4, DP fl(4.1) = +1.000001100110... 01100110 2 2, DP fl(3.1) = +1.100011001100... 11001101 2 1. Special case: if b k+1 b k+2 b k+3... = 1 0..., then add 1 to b k if and only if b k = 1. Relative rounding error: rounding to nearest (IEEE, MATLAB)) ensures fl(y) y y 1 2 ɛ mach. 5
COMPUTER NUMBERS CONTINUED Computer Arithmetic : assume denotes computer implementation of +,, / or. Model for sequence of operations: for x y, computer a) takes stored values fl(x) and fl(y), b) accurately computes fl(x) fl(y), c) then rounds the result to get fl(fl(x) fl(y)). E.g. Rounded DP for 4.1 3.1: fl(4.1) = +1.000001100110... 01100110 2 2 fl(3.1) = 0.1100011001100... 11001101 2 2 = (2 2 2 53 ) 2 2 = 1 2 51 = +.1111... 1110 = +1.1111... 11100 2 1. Matlab Checks: format long g, disp(4.1-3.1) 1 disp(sprintf( %20.16g,4.1-3.1)) 0.9999999999999996 disp( (4.1-3.1)-1 ) -4.440892098500626e-16 disp( 4.1-(3.1+1) ) 0 Computer addition is only approximately associative. IEEE requires fl(x y) (x y) x y ɛ mach, for IEEE standard computer hardware. 6
LOSS of SIGNIFICANT DIGITS Significant Digits : x approximates x to t significant digits if t is the largest nonegative integer with x x x < 5 10 t. Subtractive Cancellation : when significant digits are lost during the subtraction of two = numbers. Examples in decimal: 123.4567-123.4566 4.01 2 with 3 digits: 4.01 2 2.0025 2 2 2 = 0; Algebraic rearrangment: if is often possible to use algebraically equivalent formulas to avoid LSD calculations. Examples: a) x 2 + bx + c = 0, has x = ( b ± b 2 4c)/2, with LSD for small c/b. 7
LSD EXAMPLES b) 1/(1 + x) 1/(1 x) for small x. c) (1 sec(x))/tan 2 (x) for small x. disp([x (1-sec(x))/tan(x)^2 -cos(x)/(1+cos(x))]) Matlab Check: for x = 10.^[-2:-2:-8] end 0.01-0.499987499790956-0.499987499791664 0.0001-0.499999993627931-0.49999999875 1e-06-0.500044450290837-0.499999999999875 1e-08 0-0.5 d) (e x 1)/x for small x (Taylor series sometimes helps). 8
NUMERICAL SOFTWARE Numerical algorithms in texbook and written for class will be short algorithms, designed to illustrate primary algorithm features and problems. Software Libraries: numerical methods described and analyzed in textbook and class have been placed in software libraries. Software libraries contain carefully documented software for algorithms carefully implemented as language dependent functions and procedures: e.g. CACM, NAG, IMSL, STATLIB, WWW, for standard computer languages, and environments: e.g. MATLAB, MAPLE, MATHEMATICA, R, GAUSS for interactive work; e.g. FORTRAN, C, C++, JAVA, PYTHON for high performance computation. 9