blazing fast cis 541 numerical methods
play

Blazing Fast CIS 541 Numerical Methods Computers are extremely - PowerPoint PPT Presentation

Blazing Fast CIS 541 Numerical Methods Computers are extremely fast at performing simple arithmetic. Machine Representation of Numbers Count from one to 1,000,000,000,000 by one - or - Execute on a Pentium-4 2GHz machine:


  1. Blazing Fast CIS 541 – Numerical Methods • Computers are extremely fast at performing simple arithmetic. Machine Representation of Numbers – Count from one to 1,000,000,000,000 by one - or - – Execute on a Pentium-4 2GHz machine: int i = 1; While (1) { printf( “%d/n”, i++ ); } – Which reaches one trillion faster? August 12, 2005 OSU/CIS 541 2 Blazing Fast Blazing Fast • Assume a human can count on average one • Assume a human can count on average one number per second (bad assumption). number per second (bad assumption). • Implies 1 trillion seconds • Implies 1 trillion seconds • Or about 32,000 years!!!! • Or about 32,000 years!!!! • Assume that a 2GHz machine can increment by one each clock cycle. • Implies 500 seconds!!!! August 12, 2005 OSU/CIS 541 3 August 12, 2005 OSU/CIS 541 4

  2. Machine Precision Machine Precision • What is our current National Debt? • The point is that we live in a world with a – (see http://www.brillig.com/debt_clock/) wide range of scales. • What is the annual revenue of a Fortune 100 company? • How many molecules are in one mole of gas (under ideal • Computers have limited capabilities. assumptions) • How many pixels are displayed in a two hour HDTV • Let me repeat that: Computers have limited movie? capabilities. • What is the current balance of Bill Gate’s checking account? • How many ounces of Pepsi are consumed per year? • How many bytes of storage does the Graphics Group own? August 12, 2005 OSU/CIS 541 5 August 12, 2005 OSU/CIS 541 6 Machine Precision Accuracy • Do we really need that much accuracy? Computers have – American Express thinks so – DoD thinks so (see web article on Patriot limited missles) capabilities!!! August 12, 2005 OSU/CIS 541 7 August 12, 2005 OSU/CIS 541 8

  3. Accuracy and Range Accuracy and Range • 16-bit integers (common about 5 years ago) • 16-bit integers (common about 5 years ago) 2 16 = 65,536 2 16 = 65,536 • 32-bit integers • 32-bit integers 2 32 = 4,294,967,296 2 32 = 4,294,967,296 << 1,000,000,000,000 • Only 31-bits if you need signed integers. • Only 31-bits if you need signed integers. • 64-bit integers • 64-bit integers 2 64 ≅ 1.84 10 19 2 64 = 1.84 10 19 • Still no Avagadro’s number: 6.022*10 23 • Still no Avagadro’s number 6.022*10 23 August 12, 2005 OSU/CIS 541 9 August 12, 2005 OSU/CIS 541 10 Blazing Fast Accuracy and Standards • A 2GHZ 32-bit machine may be blazing • What is int i and what is its range? fast, but it will never reach one trillion • ANSI C standard either, if care isn’t taken!!! – int – machine dependent • Forever >> 32,000 years – short – machine dependent – Humans win!!! Not much of a standard, huh? August 12, 2005 OSU/CIS 541 11 August 12, 2005 OSU/CIS 541 12

  4. Standard Range Advice • Never use int, long in C / C++ • Corba • Use the preprocessor to define your own types – long – 32-bit signed #define CISint int \\ I want this to be 32-bit – long long – 64 bit signed #define CISshort short \\ 16-bit signed integer • Java - standardized • Hence, if you port to another architecture and need to change it, only change it in one place. • Likewise, use each packages types: glFloat, … August 12, 2005 OSU/CIS 541 13 August 12, 2005 OSU/CIS 541 14 Numbers Representing Real Numbers • Whole numbers: 0,1,2,… • In Base-10, we can express any number as • Signed integers: …,-1,0,1,2,… a sum of weighted • Rational numbers: a/b, 1/3, etc. power’s of ten. • Irrational numbers: sqrt(2), e , pi, etc. • Hence: = • 0 + • − 1 + • − 2 + • − 3 + 3.141526 3 10 1 10 4 10 1 10 K L = • 2 + • 1 + • 0 256 2 10 5 10 6 10 • Is there a difference between 0.1 and 1/3? − − 0.75 = 7 10 • 1 + • 5 10 2 = • − 0.1 1 10 1 1 −∞ ∑ = • i 3 10 3 =− i 1 August 12, 2005 OSU/CIS 541 15 August 12, 2005 OSU/CIS 541 16

  5. Representing Real Numbers Limiting Precision • Using Base-2 or binary, what are our series? • Truncate – Truncate to what? • What coefficients can we have? • Truncate to 8 significant digits. = • 8 256 1 2 – Okay: = • − + • − 1 2 3.1415926535897932384626433832795 0.75 1 2 1 2 ⇓ = • − + • − + • − + 0.1 1 2 4 1 2 5 1 2 8 L 3.1415926 1 = ? 3 August 12, 2005 OSU/CIS 541 17 August 12, 2005 OSU/CIS 541 18 Limiting Precision Error • We need to truncate rational and irrational • Round to nearest value: numbers to make them more manageable. 3.1415926535897932384626433832795 ⇓ • Absolute Error: = + ε x x % 3.1415927 • Rules: ε = − % – Typically want the absolute value: x x – If value after last retained digit is > 5, then round-up. • Relative Error: − x x % ε = – If value after last retained digit is < 5, then truncate. x – If value after last digit is =5, then: • Can I have a very small Absolute error, but • If all remaining digits are not zero, round-up. • Else, randomly round-up or truncate. a large relative error? August 12, 2005 OSU/CIS 541 19 August 12, 2005 OSU/CIS 541 20

  6. Condition of a Problem Stability of an Algorithm • For a problem with input (data) x and output • Stability indicates the sensitivity of an y , y=F(x) . The problem is said to be well- algorithm for solving a problem. conditioned if small changes in x, lead to • An algorithm is said to be stable if small small changes in y . changes in the input x lead to small changes • Otherwise, we say the problem is ill- in the output y . conditioned . • Otherwise, the algorithm is said to be unstable . August 12, 2005 OSU/CIS 541 21 August 12, 2005 OSU/CIS 541 22 Condition and Stability Precision • Condition => data x = .256834*10 5 • Stability => algorithm • The digit 2 is the most significant digit , while 4 is the least significant digit . • Ill-conditioned – very hard to get a good result • Precision should not be confused with accuracy . with even the best algorithm. Accuracy is how close your solution is to the • Stable – given good data (not ill-conditioned), the actual solution. Missed the bulls-eye by 2 inches. algorithm will not yield drastically different • Precision is how good your estimate of the results if round-off or small noise is added to the input data. accuracy is, 2.0”±0.003 August 12, 2005 OSU/CIS 541 23 August 12, 2005 OSU/CIS 541 24

  7. Precision Loss of Significance • If you miss the bulls-eye, but repeatedly hit the • Consider the error for x-y using 5 decimal same location, then it is precise, but inaccurate. digits of precision: • For instance, using Taylor’s series for the x = .3721448693 approximation of sin(x) , I can obtain a precise y =. 3720214371 x’ = .37214 number for values of x larger than 2, but the y’ =. 37202 solution would be inaccurate. x’-y’ = .00012 • In a computer, precision is usually how many x-y = .0001234322 digits of accuracy your machine representation has (number of bits in the mantissa). August 12, 2005 OSU/CIS 541 25 August 12, 2005 OSU/CIS 541 26 Loss of Significance Loss of Precision Theorem • Examine the precision loss for x-y. • The relative error is: • Let x and y be normalized floating-point − − − ( x y ) ( ' x y ') .0000034322 − = ≈ ⋅ 2 y 3 10 numbers, with x > y > 0. If − ≤ − ≤ − 2 p 1 2 q − x y .0001234322 x for some positive integers p and q , then at • However, the relative error of x’ and y’ is most p and at least q significant binary bits only 1.3*10 -5 . are lost in the subtraction. • Rule: The closer two numbers, the greater • We lost 3 significant digits. the loss of significance. August 12, 2005 OSU/CIS 541 27 August 12, 2005 OSU/CIS 541 28

  8. Avoiding loss of Precision How do they do that? • Use double precision or higher • Microsoft Windows ships with a simple calculator for its 32-bit operating system. • Modify the calculations to remove • What would you expect, if you typed in 2 96 ? subtraction of numbers close together. ( ) • Consider: as x approaches 0. – or = 2 + − 96 − f x ( ) x 1 1 + 96 2 0.3345 1 2 96 2 • Reorder to remove the subtraction: ⎛ ⎞ ( ) 2 + + 2 x 1 1 x 96 = 79,228,162,514,264,337,593,543,950,336 = + − = 2 ⎜ ⎟ f x ( ) x 1 1 – 2 ⎜ ⎟ 2 + + 2 + + x 1 1 x 1 1 ⎝ ⎠ August 12, 2005 OSU/CIS 541 29 August 12, 2005 OSU/CIS 541 30 Performance .vs. Accuracy Integers • With a 2.2GHz computer, you can do a lot of • Exact to within machine’s range: number crunching (two ops per clock cycle). – ±2 N-1 -1 • Real-time applications • See the /usr/include/limits.h file – Audio must be processed at 4KHz – One trillion time steps of crash simulation • Decide whether accuracy or performance is more important • User interactivity (e.g., calculator): – No need to calculate things faster than the user’s fat fingers can type them. • Use slower, but more accurate algorithms. August 12, 2005 OSU/CIS 541 31 August 12, 2005 OSU/CIS 541 32

Recommend


More recommend