Computing Basics. 1 Sources of Error LECTURE NOTES ECO 613/614 FALL 2007 KAREN A. KOPECKY

Similar documents
Computational Methods. Sources of Errors

Roundoff Errors and Computer Arithmetic

Chapter 3. Errors and numerical stability

Mathematical preliminaries and error analysis

Scientific Computing: An Introductory Survey

Floating-Point Arithmetic

CHAPTER 2 SENSITIVITY OF LINEAR SYSTEMS; EFFECTS OF ROUNDOFF ERRORS

Finite arithmetic and error analysis

Floating-point representation

Review of Calculus, cont d

Numerical Computing: An Introduction

Floating-Point Numbers in Digital Computers

Floating-Point Numbers in Digital Computers

MAT128A: Numerical Analysis Lecture Two: Finite Precision Arithmetic

2 Computation with Floating-Point Numbers

2 Computation with Floating-Point Numbers

Section 1.4 Mathematics on the Computer: Floating Point Arithmetic

fractional quantities are typically represented in computers using floating point format this approach is very much similar to scientific notation

2.1.1 Fixed-Point (or Integer) Arithmetic

Outline. 1 Scientific Computing. 2 Approximations. 3 Computer Arithmetic. Scientific Computing Approximations Computer Arithmetic

Lecture Notes to Accompany. Scientific Computing An Introductory Survey. What is scientific computing?

Truncation Errors. Applied Numerical Methods with MATLAB for Engineers and Scientists, 2nd ed., Steven C. Chapra, McGraw Hill, 2008, Ch. 4.

Bindel, Fall 2016 Matrix Computations (CS 6210) Notes for

Most nonzero floating-point numbers are normalized. This means they can be expressed as. x = ±(1 + f) 2 e. 0 f < 1

Scientific Computing: An Introductory Survey

Number Systems (2.1.1)

1.2 Round-off Errors and Computer Arithmetic

Review Questions 26 CHAPTER 1. SCIENTIFIC COMPUTING

What we need to know about error: Class Outline. Computational Methods CMSC/AMSC/MAPL 460. Errors in data and computation

CS321 Introduction To Numerical Methods

Computational Methods CMSC/AMSC/MAPL 460. Representing numbers in floating point and associated issues. Ramani Duraiswami, Dept. of Computer Science

COS 323: Computing for the Physical and Social Sciences

UNIVERSITY OF MASSACHUSETTS Dept. of Electrical & Computer Engineering. Digital Computer Arithmetic ECE 666

CS321. Introduction to Numerical Methods

AM205: lecture 2. 1 These have been shifted to MD 323 for the rest of the semester.

Floating Point Arithmetic

Numerical Methods 5633

Math 340 Fall 2014, Victor Matveev. Binary system, round-off errors, loss of significance, and double precision accuracy.

Error in Numerical Methods

Floating Point Arithmetic

Chapter 2. Data Representation in Computer Systems

1.3 Floating Point Form

CSCI 402: Computer Architectures. Arithmetic for Computers (3) Fengguang Song Department of Computer & Information Science IUPUI.

More Programming Constructs -- Introduction

Computational Economics and Finance

Module 2: Computer Arithmetic

Binary floating point encodings

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

Floating-Point Arithmetic

Errors in Computation

Accuracy versus precision

ME 261: Numerical Analysis. ME 261: Numerical Analysis

Computer Arithmetic Floating Point

Computer Arithmetic. 1. Floating-point representation of numbers (scientific notation) has four components, for example, 3.

Lecture 03 Approximations, Errors and Their Analysis

CS101 Lecture 04: Binary Arithmetic

COS 323: Computing for the Physical and Social Sciences

AMTH142 Lecture 10. Scilab Graphs Floating Point Arithmetic

COMP2611: Computer Organization. Data Representation

Number Systems. Both numbers are positive

Today CISC124. Building Modular Code. Designing Methods. Designing Methods, Cont. Designing Methods, Cont. Assignment 1 due this Friday, 7pm.

Floating Point Representation in Computers

Data Representation Type of Data Representation Integers Bits Unsigned 2 s Comp Excess 7 Excess 8

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

What Every Programmer Should Know About Floating-Point Arithmetic

Introduction to Computers and Programming. Numeric Values

Class 5. Data Representation and Introduction to Visualization

Lecture Objectives. Structured Programming & an Introduction to Error. Review the basic good habits of programming

Computational Methods CMSC/AMSC/MAPL 460. Representing numbers in floating point Error Analysis. Ramani Duraiswami, Dept. of Computer Science

Floating Point Considerations

Computational Economics and Finance

Floating Point Representation. CS Summer 2008 Jonathan Kaldor

Computational Methods CMSC/AMSC/MAPL 460. Representing numbers in floating point and associated issues. Ramani Duraiswami, Dept. of Computer Science

3.1 DATA REPRESENTATION (PART C)

Data Representation and Introduction to Visualization

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

Floating point. Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties. Next time. !


Introduction to floating point arithmetic

UNIVERSITY OF MASSACHUSETTS Dept. of Electrical & Computer Engineering. Digital Computer Arithmetic ECE 666

arxiv:cs/ v1 [cs.ds] 11 May 2005

Scientific Computing. Error Analysis

Introduction to Numerical Computing

Lecture Notes: Floating-Point Numbers

Chapter 4. Operations on Data

Classes of Real Numbers 1/2. The Real Line

CHAPTER 5: Representing Numerical Data

Independent Representation

The ALU consists of combinational logic. Processes all data in the CPU. ALL von Neuman machines have an ALU loop.

Floating Point. CSC207 Fall 2017

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

NUMERICAL ANALYSIS USING SCILAB: NUMERICAL STABILITY AND CONDITIONING

EE878 Special Topics in VLSI. Computer Arithmetic for Digital Signal Processing

Computational Mathematics: Models, Methods and Analysis. Zhilin Li

The Design and Implementation of a Rigorous. A Rigorous High Precision Floating Point Arithmetic. for Taylor Models

Floating-Point Arithmetic

UNIVERSITY OF MASSACHUSETTS Dept. of Electrical & Computer Engineering. Digital Computer Arithmetic ECE 666

Lecture 17 Floating point

Basics of Computation. PHY 604:Computational Methods in Physics and Astrophysics II

Data Representation Floating Point

Transcription:

LECTURE NOTES ECO 613/614 FALL 2007 KAREN A. KOPECKY Computing Basics 1 Sources of Error Numerical solutions to problems differ from their analytical counterparts. Why? The reason for the difference is that most numerical computations involve error. There are two main types of error that can arise. The first, called roundoff error, arises from the fact that computers do not store real numbers instead they store a subset of real numbers: floatingpoint numbers. Thus the solution of computer arithmetic is approximated with the nearest machine-representable number. The second, called truncation error, arises from truncating infinite series, substituting for continuous functions with discrete approximations, and approximating limits of infinite processes. There are two statistics used to measure error: absolute error and relative error. Suppose a real number x is represented in the computer by the floating-point number x, then the absolute roundoff error is x x and the relative roundoff error is x x. Similarly, sup- x pose we approximate a function f by f, then the absolute truncation error from evaluating f at some point x is f(x) f(x) and the relative truncation error is f(x) f(x) f(x). Most computations will involve a combination of truncation and roundoff error. For example suppose we want to evaluate some function, f( ) at some real number x numerically. In order to do this we approximate f( ) with f( ) and the computer stores our real number x as the floating-point number x. We can bound the total error, either absolute or relative, by the sum of the roundoff error and the truncation error: total error = f( x) f(x) f( x) f( x) + f( x) f(x). (1) }{{}}{{} truncation error roundoff error Good computer programs keep total error, or the upper bound on total error that is determined by the sum of roundoff error and truncation error, as small as possible given 1

constraints on computation time and memory space. Roundoff error is a characteristic of computer hardware. For this type of error the most important thing is to keep it from propagating. Truncation error, on the other hand, is a characteristic of the algorithm or numerical procedure being employed. Often times truncation error can be reduced by increasing the accuracy of approximations. But this improvement comes at a cost of either memory space or time. This is the tradeoff between accuracy and efficiency. To see how numerical error can grow we must first understand how the computer stores its approximations of real numbers. Floating-point numbers are stored on a computer in binary (base 2) form. Basically, a floating point number is a collection of bits (0 s and 1 s). Most computation today is done in double precision which means that each floating point number consists of 2 words. A word is a collection of bits. On standard computers today 1 word = 4 bytes or 32 bits (1 byte = 8 bits). This means that floating point numbers are stored using 64 bits each, set to either 0 or 1. These bits are split up between what s called the number s mantissa and the number s exponent. In addition, a few bits are set aside to store the sign of the number and possibly other characteristic flags (such as whether it is zero or undefined ). Thus the value of the floating point number can be expressed as N d i 2 i 2 E, (2) i=0 }{{} mantissa where E is the exponent, d i {0, 1}, and N is the maximum number of 0 s and 1 s in the number. Another way to write this is [d 1 2 1 + d 2 2 2 + + d N 2 N ] 2 E = [.d 1 d 2... d N ] 2 E This may be easier to see if we consider the equivalent representation in base 10: where s i {0, 1,..., 9}. Or N s i 10 i 10 E, (3) i=0 } {{ } mantissa [s 1 10 1 + s 2 10 2 + + s N 10 N ] 10 E = [.s 1 s 2... s N ] 10 E Notice that allocating more bits to storing E will increase the range of numbers the computer can handle, while increasing N will increase the degree of precision of calculations. 2

Notice that the smallest number such that when added to 1 the computer can tell is no longer 1 is 2 N. This is also the smallest relative spacing between two numbers that the computer can distinguish. It is called machine epsilon. For double-precision N is usually 52 or 53 thus machine-epsilon is 2 52 or 2 53 which is approximately 10 16. This value is also sometimes referred to as unit roundoff and here we will denote it by u. Thus, for doubleprecision, any real number can be approximated to a relative accuracy of 10 16, or, in other words, the real number and its computer floating-point approximation will agree up to at least the fifteenth digit. Every time an arithmetic computation is performed on a computer the result is approximated by a floating-point number. In some cases this can lead to large roundoff errors. One example is the subtraction of a number from another of similar value. The error is worse the larger are the two numbers. Suppose for instance that x and y are two large real numbers with very similar values. It must be that x x u x and y ỹ u y where u is the unit roundoff. Also We can bound the absolute error as follows (x y) (x y) u x y (x y) ( x ỹ) = (4) (x y) ( x ỹ) + ( x ỹ) ( x ỹ) (x y) ( x ỹ) + ( x ỹ) ( x ỹ) (5) So the relative error is bound by x x + y ỹ + ( x ỹ) ( x ỹ) (6) u x + u y + u x ỹ (7) u x + u y + u x y 2u x + 2u y (8) 2u x + 2u y x y 4 x u x y 3

which could be quite large since x >> x y. Another way to see this is to consider that, x and y, are accurate to n digits and have the first ñ digits in common. Then their difference will have approximately n ñ significant digits. The number of significant digits in the difference is smaller the more digits that x and y have in common. Another example of how arithmetic computations can result in big roundoff error is when very large numbers are multiplied by very small numbers, magnifying the approximation error of the small number. Two general rules of thumb are (1) avoid taken the difference of two very similar numbers when possible and (2) avoid multiplying very large numbers by very small numbers. Roundoff error can also become significant when the results of computations are out of the range of floating-point numbers the computer can store. While these numbers vary somewhat across processors and programs, for double-precision computing, the smallest positive number that can be represented on the computer, termed machine zero, is approximately 10 308, while the largest number, termed machine infinity, is approximately 10 308. This is because there are usually about 11 bits reserved for the exponents. Which means 2 11 possible values for exponent or 2048 but half are for positive and half for negative so the largest number is approximately 2 1024 10 308. It is possible to get output of computer computations done on machine-representable numbers that exceed machine infinity in value. This event is called overflow. Similarly, when output of computer computations results in a positive, non-zero, but less than machine epsilon value, it is called underflow. 2 Conditioning and Stability At various steps in a computational algorithm roundoff error and truncation error are introduced. The impact that this error has on the difference between the true solution to the initial problem and the solution resulting from the computation will depend on both the sensitivity of the problem and the sensitivity of the algorithm to the introduction of error. Conditioning is a property of the problem. A problem in which a given relative change in the value of the inputs results in a similar relative change in the output value is said to be insensitive or well-conditioned. Problems that are well-conditioned do not cause errors to propagate since the relative error of the output will be on the same scale as the relative 4

error of inputs. Problems in which small relative changes in inputs leads to large relative changes in the output are said to be ill-conditioned or sensitive. An example of an illconditioned problem is 1.00001 1 x 1 = 2.00001. (9) 1 1 2 x 2 Its exact solution is x = (1, 1) but if we change the first element on the right-hand-side from 2.00001 to 2 the solution changes drastically to x = (0, 2). One measure of ill conditioning in a linear equation Ax = b is the elasticity of the solution vector x with respect to the data vector b: ɛ = x / x sup b >0 b / b. The elasticity gives the maximum percentage change in the size of the solution vector x induced by a 1 percent change in the size of the data vector b. If the elasticity is large, then small errors in b can result in large errors in x. The elasticity is expensive to compute and is instead estimated using the condition number of the matrix A. For invertible A this is κ A A 1. The condition number of A is the least upper bound of the elasticity. Rule of thumb is that for each power of 10 in the condition number, one significant digit is lost in the computed solution vector x. Stability is a property of the algorithm. An algorithm is said to be stable if its outcome is relatively insensitive to roundoff error. Thus the accuracy of numerical procedures depends on both the conditioning of the problem and the stability of the algorithm. In other words, when numerical solutions to problems differ greatly from their analytical counterpart the reason could be that the problem is ill-conditioned or that the numerical procedure is unstable or both. Side note: In practice, when numerical solutions to problems are not as expected the majority of the time the culprit is human error, i.e., there is either a mistake in the code or there is a mistake in the setup of the problem. Thus this is always the first possibility you should explore. 5

References Judd, Kenneth L. 1998. Numerical Methods in Economics. Cambridge, MA: MIT Press. Nocedal, Jorge and Stephen J. wright. 1999. Numerical Optimization. Springer- Verlag New York, Inc. 6