CSCI 125 & 161 Lecture 13 Martin van Bommel. Floating Point Data Floating point numbers are not exact Value 0.1 in binary is very close to 1/10, but not.

Slides:



Advertisements
Similar presentations
Part 1 Chapter 4 Roundoff and Truncation Errors PowerPoints organized by Dr. Michael R. Gustafson II, Duke University All images copyright © The McGraw-Hill.
Advertisements

Roundoff and truncation errors
CSE 330: Numerical Methods
1 Chapter Six Algorithms. 2 Algorithms An algorithm is an abstract strategy for solving a problem and is often expressed in English A function is the.
KS3 Mathematics N4 Powers and roots
Numerical Computation
Introduction to Programming (in C++) Numerical methods I Jordi Cortadella Dept. of Computer Science, UPC.
Error Measurement and Iterative Methods
ECIV 201 Computational Methods for Civil Engineers Richard P. Ray, Ph.D., P.E. Error Analysis.
Approximations and Errors
COS 323: Computing for the Physical and Social Sciences Szymon Rusinkiewicz.
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 6 Roots of Equations Bracketing Methods.
COS 323: Computing for the Physical and Social Sciences Szymon Rusinkiewicz.
1 Error Analysis Part 1 The Basics. 2 Key Concepts Analytical vs. numerical Methods Representation of floating-point numbers Concept of significant digits.
Calculus I – Math 104 The end is near!. 1. Limits: Series give a good idea of the behavior of functions in the neighborhood of 0: We know for other reasons.
Computer Arithmetic Integers: signed / unsigned (can overflow) Fixed point (can overflow) Floating point (can overflow, underflow) (Boolean / Character)
CISE-301: Numerical Methods Topic 1: Introduction to Numerical Methods and Taylor Series Lectures 1-4: KFUPM.
Chapter 2: Equations and Inequalities 2.4: Other Types of Equations
Python Programming: An Introduction to Computer Science
First, some left-overs… Lazy languages Nesting 1.
Vahé Karamian Python Programming CS-110 CHAPTER 3 Computing with Numbers.
Chapter 9 Sequences and Series The Fibonacci sequence is a series of integers mentioned in a book by Leonardo of Pisa (Fibonacci) in 1202 as the answer.
Numerical Computations in Linear Algebra. Mathematically posed problems that are to be solved, or whose solution is to be confirmed on a digital computer.
Chapter 1 Infinite Series, Power Series
Lecture 2 Number Representation and accuracy
CISE-301: Numerical Methods Topic 1: Introduction to Numerical Methods and Taylor Series Lectures 1-4: KFUPM CISE301_Topic1.
CISE301_Topic11 CISE-301: Numerical Methods Topic 1: Introduction to Numerical Methods and Taylor Series Lectures 1-4:
Numerical Methods Applications of Loops: The power of MATLAB Mathematics + Coding 1.
5.2 Errrors. Why Study Errors First? Nearly all our modeling is done on digital computers (aside: what would a non-digital analog computer look like?)
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
Copyright 2013, 2009, 2005, 2002 Pearson, Education, Inc.
Approximate computations Jordi Cortadella Department of Computer Science.
Additional Problems.
Loop Application: Numerical Methods, Part 1 The power of Matlab Mathematics + Coding.
MA/CS 375 Fall MA/CS 375 Fall 2002 Lecture 31.
NUMERICAL ERROR Student Notes ENGR 351 Numerical Methods for Engineers Southern Illinois University Carbondale College of Engineering Dr. L.R. Chevalier.
Floating Point Arithmetic
MECN 3500 Inter - Bayamon Lecture 3 Numerical Methods for Engineering MECN 3500 Professor: Dr. Omar E. Meza Castillo
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Chapter 3.
This is an example of an infinite series. 1 1 Start with a square one unit by one unit: This series converges (approaches a limiting value.) Many series.
Numerical Methods Solution of Equation.
Numerical Analysis CC413 Propagation of Errors.
CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.
Computer Science 112 Fundamentals of Programming II.
CSCI 125 & 161 Lecture 12 Martin van Bommel. Prime Numbers Prime number is one whose only divisors are the number 1 and itself Therefore, number is prime.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
FUNCTIONS The parts of a function In order to work correctly, a function must be written a specific way, which is called the syntax. The basic syntax for.
Numerical Analysis CC413 Propagation of Errors. 2 In numerical methods, the calculations are not made with exact numbers. How do these inaccuracies propagate.
NUMERICAL ANALYSIS I. Introduction Numerical analysis is concerned with the process by which mathematical problems are solved by the operations.
Numerical Methods Some example applications in C++
Approximate computations
CSE 575 Computer Arithmetic Spring 2003 Mary Jane Irwin (www. cse. psu
Approximate computations
Computational Methods EML3041
Chapter 2 ERROR ANALYSIS
Maths Unit 9 – Forming & Solving Equations
Roundoff and Truncation Errors
Solution of Equations by Iteration
Approximations and Round-Off Errors Chapter 3
Applications of Expansion and Factorisation
Section 4.8: Newton’s Method
3.8: Newton’s Method Greg Kelly, Hanford High School, Richland, Washington.
3.8: Newton’s Method Greg Kelly, Hanford High School, Richland, Washington.
Roundoff and Truncation Errors
Number Systems Unit Review Day 1.
Errors and Error Analysis Lecture 2
CISE-301: Numerical Methods Topic 1: Introduction to Numerical Methods and Taylor Series Lectures 1-4: KFUPM CISE301_Topic1.
Presentation transcript:

CSCI 125 & 161 Lecture 13 Martin van Bommel

Floating Point Data Floating point numbers are not exact Value 0.1 in binary is very close to 1/10, but not precisely equal to it = Cannot rely on accuracy of floating point values

“for” With Floating Point Data Following “for” loop is syntactically correct for (x = 1.0; x <= 2.0; x += 0.1)... On some machines, x will never take on value 2.0, but will become , which fails the test

for With Floating Point Values Better to use the for loop for (i = 10; i <= 20; i++)... Then use the calculation x = i / 10.0; to give the floating point values for x

Equality With Floating Points Testing floating point data for equality leads to problems with precision Want to test if equal within some epsilon In reality, test if absolute value of difference between numbers, divided by the smaller of their absolute values, is less than 

Approximately Equal #define Epsilon bool ApproximatelyEqual(double x, double y) { double diff = abs(x - y); y = abs(y); x = abs(x); if (y < x) x = y; return ((diff / x) < Epsilon); }

Numerical Algorithms Techniques used by computers to implement mathematical functions like sqrt sqrt function exists in math library Most people just use it How is it implemented? –Successive approximation? –Series expansion?

Successive Approximation Steps: 1. Make a guess at the answer 2. Use guess to generate a better answer 3. If getting closer to actual answer, repeat until guess is close enough How to generate next guess? –need a converging sequence of guesses

Newton’s Method e.g. –Suppose want square root of 16 –Use 8 as first guess - too large, 8 x 8 = 64 –Derive next guess by dividing value by guess –16 / 8 = 2, answer must be between 2 and 8 –Use (2 + 8) / 2 = 5 as next guess –16 / 5 = 3.2, ( ) / 2 = 4.1 as next guess –Next guesses: and

Problem with Successive Approx Process will never guarantee an exact answer Can continue until close enough (?) Use ApproximatelyEqual function Continue until square of guess approx. equal to original number

Newton’s Algorithm double Sqrt(double x) { double g = x; if (x == 0) return 0; if (x < 0) {cout << ”Sqrt on negative number\n”); return 0; } while (!ApproximatelyEqual(x, g*g)) g = (g + x / g) / 2.0; return g; }

Series Expansion Value of function approximated by adding terms in a mathematical series If addition of new term brings total closer to desired value, series converges and can be used to approximate result Taylor Series Approximation for 0 < x < 2

Implement Series Expansion For a series such as: Think of each inside term as Initially sign = odd = 1; Then, for the next term sign = -sign; odd = odd + 2;

Maclaurin Series for e^x Think of each inside term as Initially xpow = xfact = 1 Then to get the next term (i) xpow = xpow * xfact = fact * i

Implementing Taylor Series Think of each term as Then, for next term xpower *= (x-1); factorial *= (i+1); coeff *= (0.5-i);

Taylor Algorithm double TaylorSqrt(double x) { double sum, factorial, coeff, term, xpower; int i; factorial = coeff = xpower = term = 1.0; sum = 0.0; for (i = 0; sum != sum + term; i++) { sum += term; coeff *= (0.5 - i); xpower *= (x - 1); factorial *= (i + 1); term = coeff * xpower / factorial; } return sum; }

Fixing Limitation Taylor Series Approx works for 0 < x < 2 How can we use it for larger x? Recall Then divide by 4 until within range, then calcuate sqrt, and multiply by 2’s to recover e.g. sqrt(24) = sqrt(4*4*1.5) = 2*2*sqrt(1.5)

Taylor Fix double TSqrt(double x) { int mult = 1; if (x == 0) return (0); if (x < 0) { cout << "TSqrt of negative value " << x << endl; return 0; } while (x >= 2){ x /= 4; mult *= 2; } return mult * TaylorSqrt(x); }