Docsity

Arithmetic and Error - Lecture Slides | CMSC 460, Study notes of Computer Science

Toggle sidebar
bg1
1
1999 - 2006 Dianne P. O'Leary 1
Notes for Part 1 of
CMSC 460
Preliminaries:
• Mathematical modeling
• Computer arithmetic
• Errors
Dianne P. O’Leary
1999 - 2006 Dianne P. O'Leary 2
Arithmetic and Error
What we need to know about error:
-- how does error arise
-- how machines do arithmetic
-- fixed point arithmetic
-- floating point arithmetic
-- how errors are propagated in calculations.
-- how to measure error
1999 - 2006 Dianne P. O'Leary 3
How does error arise?
1999 - 2006 Dianne P. O'Leary 4
How does error arise?
Example: An engineer wants to study the stresses
in a bridge.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Arithmetic and Error - Lecture Slides | CMSC 460 and more Study notes Computer Science in PDF only on Docsity!

1999 - 2006^

Dianne P. O'Leary

1

Notes for Part 1 ofCMSC 460^ Dianne P. O’Leary^ Preliminaries:• Mathematical modeling• Computer arithmetic• Errors

1999 - 2006^

Dianne P. O'Leary

2

Arithmetic and Error^ What we need to know about error:-- how does error arise-- how machines do arithmetic-- fixed point arithmetic-- floating point arithmetic-- how errors are propagated in calculations.-- how to measure error

1999 - 2006^

Dianne P. O'Leary

3

How does error arise?

1999 - 2006^

Dianne P. O'Leary

4

How does error arise?^ Example: An engineer wants to study the stressesin a bridge

1999 - 2006^

Dianne P. O'Leary

5

Measurement error^ Step 1: Gather lengths, angles, etc. for girders and wires.

1999 - 2006^

Dianne P. O'Leary

6

Modeling error^ Step 2: Approximate system by finite elements.

1999 - 2006^

Dianne P. O'Leary

7

Truncation error^ Step 3: Numerical analyst develops an algorithm: the stresscan be computed as the

limit^ (as n becomes infinite) of some function G(n).Can’t take this limit on a computer, so decide to use G(150).^ G(1), G(2),

G(3), G(4), G(5), ...

1999 - 2006^

Dianne P. O'Leary

8

Roundoff error Step 4: The algorithm is programmed and run on a computer.We need^

π. Approximate it by 3.1415926. 3 1 4 1

1999 - 2006^

Dianne P. O'Leary

13

How machines doarithmetic

1999 - 2006^

Dianne P. O'Leary

14

Machine Arithmetic:

Fixed Point

How integers are stored in computers:Each^ word

(storage location) in a machine contains a fixed number of digits.Example: A machine with a 6-digit word might represent

1985 as^0

1999 - 2006^

Dianne P. O'Leary

15

Fixed Point:

Decimal vs. Binary^0

Most calculators use

decimal (base 10)

representation.

Each digit is an integer between 0 and 9.The value of the number is1 x 10 + 9 x 10 + 8 x 10 + 5 x 10

1999 - 2006^

Dianne P. O'Leary

16

Fixed Point:

Decimal vs. Binary^0

Most computers use

binary (base 2)

representation.

Each digit is the integer 0 or 1.If the number above is binary, its value is1 x 2 + 0 x 2 + 1 x 2 + 1 x 2 + 0 x 2

. (or 22 in base 10)

4 3

1999 - 2006^

Dianne P. O'Leary

17

Example of binary addition^0

+_______________________^ Note the “

carry ” here!

(binary) = 10

1 + 1 + 1 = 11 (binary)=

1999 - 2006^

Dianne P. O'Leary

18

Example of binary addition^0

+_______________________^ Note the “

carry ” here!

In decimal notation,

1999 - 2006^

Dianne P. O'Leary

19

Representing negativenumbers^0

Computers represent negative numbers using“one’s complement”, “two’s complement”,or sign-magnitude representation. Sign magnitude

is easiest, and enough for us: if the first bit is zero, then the number is positive.Otherwise, it is negative.

Denotes^

Denotes^

1999 - 2006^

Dianne P. O'Leary

20

Range of fixed point numbers Largest 5-digit (5 bit) binary number:

1999 - 2006^

Dianne P. O'Leary

25

Features of fixed pointarithmetic^ Easy: always get an integer answer.Either we get exactly the right answer for addition,subtraction, or multiplication, or we can detect overflow.The numbers that we can store are equally spaced.Disadvantage:

very^ limited range of numbers.

1999 - 2006^

Dianne P. O'Leary

26

Floating point

arithmetic

If we wanted to store 15 x 2

11 , we would need 16 bits: Instead, let’s agree to code numbers as

two^ fixed point

numbers:

z x 2^ ,

with z = 15 saved as 01111and p = 11 saved as 01011. Now we can have fractions, too:

binary .101 = 1 x 2 + 0 x 2

  • 1 x 2. p

-1^ -

1999 - 2006^

Dianne P. O'Leary

27

Floating point

arithmetic

Jargon:^ z is called the

mantissa

or^ significand

p is called the

exponent

p ± z x 2

To make the representation unique (since, for example,^1 2 x 2= 4 x 2

0 ), we make the rule that

1 ≤^ z < 2

(normalization

We store^

d digits^ for the mantissa, and limit the range of the exponent to

m^ ≤^ p^ ≤ M, for some integers m and M.

1999 - 2006^

Dianne P. O'Leary

28

Floating point representation^ Example: Suppose we have a machine with d = 5, m = -15,

M = 15.

(^10) 15 x 2= 1111

(^10) x 2= 1.111 2

(^13) x 2 2 mantissa^

z = +1. exponent^

p = + -10^ 15 x 2 = 1111x 2^2

-10^ = 1.

-7x 2 2 mantissa^

z = +1. exponent^

p = -

1999 - 2006^

Dianne P. O'Leary

29

Floating point

standard

Up until the mid-1980s, each computer manufacturer had adifferent choice for d, m, and M, and even a different wayto select answers to arithmetic problems.A program written for one machine often would not computethe same answers on other machines.The situation improved somewhat with the introduction in 1987of^ IEEE standard

floating point arithmetic.

1999 - 2006^

Dianne P. O'Leary

30

Floating point

standard

On most machines today,single precision:

d = 24, m = -126, M = 127 double precision: d = 53, m = -1022, M = 1023.(And the representation is 2’s complement, notsign-magnitude, so that the number -|x| is stored as 2

d^ - |x|,

where d is the number of bits allotted for its representation.)

1999 - 2006^

Dianne P. O'Leary

31

Floating point

addition

Machine arithmetic is more complicated for floating point.Example: In fixed point, we added 3 + 10.Here it is in floating point:3 =^ 11 (binary) = 1.100 x 2

1 z = 1.100,

p = 1

10 = 1010 (binary) = 1.010 x 2

3 z = 1.010,

p = 11.

  1. Shift the smaller number so that the exponents are equalz = 0.

p = 11

  1. Add the mantissasz = 0.0110 + 1.010 = 1.1010,

p = 11

  1. Shift if necessary to normalize.

1999 - 2006^

Dianne P. O'Leary

32

Roundoff in Floating pointaddition Sometimes we cannot store the exact answer.Example: 1.1001 x 2

0 + 1.0001 x 2

    1. Shift the smaller number so that the exponents are equalz = 0.

p = 0

  1. Add the mantissas

0.10001+ 1.1001= 10.00011,^

p = 0

  1. Shift if necessary to normalize: 1.000011 x 2

1

But we can only store 1.0000 x 2

1! The error is called

roundoff

1999 - 2006^

Dianne P. O'Leary

37

Features of floating point arithmetic^ • The numbers that we can store are

not^ equally

spaced. (Try to draw them on a number line.)• A wide range of variably-spaced numbers can berepresented exactly.• For addition, subtraction, and multiplication, eitherwe get exactly the right answer or a rounded versionof it, or we can detect underflow or overflow.

1999 - 2006^

Dianne P. O'Leary

38

How errors are propagated

1999 - 2006^

Dianne P. O'Leary

39

Numerical Analysis vs.Analysis^ Mathematical analysis works with computations involving^ real^ or^ complex

numbers. Computers do not work with these; for instance, theydo not have a representation for the numbers

π^ or e or

even 0.1 .Dealing with the finite approximations called

floating point

numbers means that we need to understand

error and

its propagation.

1999 - 2006^

Dianne P. O'Leary

40

Absolute vs. relative errors^ Absolute error

in c as an approximation to x:|x – c| Relative error

in c as an approximation to nonzero x:|x –^ c|^ |x|

1999 - 2006^

Dianne P. O'Leary

41

Error Analysis Errors can be magnified during computation.Example:^

2.003^ x^

0 10 (suppose

±^ .001 or .05% error)

  • 2.000^ x

0 10 (suppose

±^ .001 or .05% error)

Result of subtraction:

0.003^ x^

but true answer could be as small as 2.002 - 2.001 = 0.001,

or as large as

1999 - 2006^

Dianne P. O'Leary

42

Error Analysis Errors can be magnified during computation.Example:^

2.003^ x^

0 10 (suppose

±^ .001 or .05% error)

  • 2.000^ x

0 10 (suppose

±^ .001 or .05% error)

Result of subtraction:

0.003^ x^

0 10 (±^ .002 or 200% error if trueanswer is 0.001) Catastrophic cancellation

, or “loss of significance”

1999 - 2006^

Dianne P. O'Leary

43

Error Analysis^ We could generalize this example to prove a theorem:^ When^ adding or subtracting

, the bounds on

absolute errors

add.

1999 - 2006^

Dianne P. O'Leary

44

Error Analysis^ What if we multiply or divide?Suppose x and y are the true values, and X and Y are ourapproximations to them.

If X = x (^

-^ r)^ and^

Y = y (^

-^ s)

then r is the relative error in x and s is the relative errorin y. You could show thatxy - XYxy^

≤^ |r| + |s| + |rs

1999 - 2006^

Dianne P. O'Leary

49

Avoiding cancellation 2)^ Rewrite the formula. sqrt(x + e) - sqrt(x) = (sqrt(x+e) - sqrt(x)) (sqrt(x+e) + sqrt(x))

(sqrt(x+e) + sqrt(x)) =^ x + e - x

=^

e

sqrt(x+e) + sqrt(x)

sqrt(x+e) + sqrt(x)

so^ x= 28 - sqrt(783) = sqrt(784) - sqrt(783).^2

1999 - 2006^

Dianne P. O'Leary

50

Avoiding cancellation 3)^ Use Taylor series.^ Let^ f(x) = sqrt(x).

Then f(x+a) - f(x) = f

′(x) a + 1/2 f

(^2) ″(x) a+ ...

1999 - 2006^

Dianne P. O'Leary

51

How errors are measured

1999 - 2006^

Dianne P. O'Leary

52

Error analysis^ Error analysis

determines the cumulative effects of error. Two approaches:• Forward error analysis• Backward error analysis

1999 - 2006^

Dianne P. O'Leary

53

1)^ Forward error analysis^ This is the way we have been discussing.Find an estimate for the answer, and bounds on the error.^ Space of problems

Space of answers

1999 - 2006^

Dianne P. O'Leary

54

1)^ Forward error analysis^ This is the way we have been discussing.Find an estimate for the answer, and bounds on the error.^ Space of problems

Space of answers

Trueproblem(known)

Truesolution (unknown)

1999 - 2006^

Dianne P. O'Leary

55

1)^ Forward error analysis^ This is the way we have been discussing.Find an estimate for the answer, and bounds on the error.^ Space of problems

Space of answers

Trueproblem(known)

Truesolution#(unknown) Computedsolution

1999 - 2006^

Dianne P. O'Leary

56

1)^ Forward error analysis^ This is the way we have been discussing.Find an estimate for the answer, and bounds on the error.^ Space of problems

Space of answers

Trueproblem(known)

(known)region#guaranteedto containtrue soln. Computedsolution

1999 - 2006^

Dianne P. O'Leary

61

Arithmetic and Error^ Summary:-- how does error arise-- how machines do arithmetic-- fixed point arithmetic-- floating point arithmetic-- how errors are propagated in calculations.-- how to measure error