CSE202: Lecture 10AThe Ohio State University1 Numerical Error.

Slides:



Advertisements
Similar presentations
1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
Advertisements

CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
Introduction to Computers and Programming Lecture 7:
Overview Order of presentation different than published handouts Run a program on ccc Finish Arithmetic operations Data types integer char floating point.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Data types and variables
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
CSE202: Lecture 8The Ohio State University1 Formatting Numbers for Output.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Basic Elements of C++ Chapter 2.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Information Representation (Level ISA3) Floating point numbers.
Input & Output: Console
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
C Tokens Identifiers Keywords Constants Operators Special symbols.
CSE1222: Lecture 4The Ohio State University1. Mathematical Functions (1)  The math library file cmath Yes, this is a file with definitions for common.
CSE202: Lecture 4The Ohio State University1 Mathematical Functions.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CSE1222: Lecture 3The Ohio State University1. Assignment Operations  The C++ assignment operator is: =  Examples: x = 3 * 5; y = x – 7; y = y + 4; Do.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
Unit 3 Lesson 4 How Data Types Affect Calculations Dave Clausen La Cañada High School.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 3: Assignment, Formatting, and Interactive Input.
Simple Data Types Built-In and User Defined Chapter 10.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Top-Down Stepwise Refinement (L11) * Top-Down Stepwise Refinement * Cast Operator * Promotion (Implicit Conversion) * Unary Operator * Multiplicative Operator.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
1 Objects Types, Variables, and Constants Chapter 3.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
The Ohio State University
Chapter Topics The Basics of a C++ Program Data Types
Programming Fundamentals
Tokens in C Keywords Identifiers Constants
Basic Elements of C++.
Programming Fundamentals
Basic Elements of C++ Chapter 2.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Program Breakdown, Variables, Types, Control Flow, and Input/Output
C++ Programming Lecture 3 C++ Basics – Part I
C++ for Engineers and Scientists Second Edition
Presentation transcript:

CSE202: Lecture 10AThe Ohio State University1 Numerical Error

CSE202: Lecture 10AThe Ohio State University2 Data Types: Floating Point Numbers (1) Floating-point numbers have a decimal point, and they can also be signed or unsigned. There are three types: float, double, or long double. –The differences between these are their supported range and precision.

CSE202: Lecture 10AThe Ohio State University3 Data Types: Floating Point Numbers (2) To represent a floating point number: –float uses 32 bits (4 bytes) –double uses 64 bits (8 bytes) –long double uses 128 bits (16 bytes) The tradeoff is storage vs. precision and range What exactly is the precision and range, and how are floating point numbers represented in binary format? IEEE 754 Standard

CSE202: Lecture 10AThe Ohio State University4 Numerical Error (1) // example of numerical error #include using namespace std; int main() { double x(0.7); // initialize x to 0.7 cout << "( )* = " << (x-0.6)* << endl; return 0; }

CSE202: Lecture 10AThe Ohio State University5 > numerical_error1.exe ( )* = e-16 > … double x(0.7); // initialize x to 0.7 cout << "( )* = " << (x-0.6)* << endl; …

CSE202: Lecture 10AThe Ohio State University6 Numerical Error (2) // print 18 significant digits #include using namespace std; int main() { double x(0.7); // initialize x to 0.7 cout << "x = " << x << endl; cout.precision(18); // output 18 significant digits cout << "x = " << x << endl; return 0; }

CSE202: Lecture 10AThe Ohio State University7 > numerical_error2.exe x = 0.7 x = > … double x(0.7); // initialize x to 0.7 cout << "x = " << x << endl; cout.precision(18); // output 18 significant digits cout << "x = " << x << endl; …

CSE202: Lecture 10AThe Ohio State University8 Numerical Error Computers store floating point numbers in binary (base 2 or as 0’s and 1’s). There is no way to represent 0.7 precisely in base 2. There is no way to represent 0.1 precisely in base 2!

CSE202: Lecture 10AThe Ohio State University9 Numerical Error (3)... int main() { cout.precision(18); // output 18 significant digits cout << "0.0 = " << 0.0 << endl; cout << "0.1 = " << 0.1 << endl; cout << "0.2 = " << 0.2 << endl; cout << "0.3 = " << 0.3 << endl; cout << "0.4 = " << 0.1 << endl; cout << "0.5 = " << 0.5 << endl; cout << "0.6 = " << 0.6 << endl; cout << "0.7 = " << 0.7 << endl; cout << "0.8 = " << 0.8 << endl; cout << "0.9 = " << 0.9 << endl; cout << "1.0 = " << 1.0 << endl; return 0; }

CSE202: Lecture 10AThe Ohio State University10 > numerical_error3.exe 0.0 = = = = = = = = = = = 1 > … cout.precision(18); // output 18 significant digits cout << "0.0 = " << 0.0 << endl; cout << "0.1 = " << 0.1 << endl; …

CSE202: Lecture 10AThe Ohio State University11 Floating Point Accuracy in Conditional Expressions

CSE202: Lecture 10AThe Ohio State University12 Floating Point Accuracy Issue (1) WARNING: when comparing floating point numbers (of any kind: float, double, long double, …) you cannot use the == operator reliably This is a computer limitation. Two numbers, which should be equal, may not be stored as precisely equal in the computer. –Remember numerical accuracy: how would 0.1 be represented in binary notation? It is not possible to add negative powers of 2’s to get an exact representation of 0.1 –Instead, we only get a very good approximation but is still NOT exactly equal to 0.1

CSE202: Lecture 10AThe Ohio State University13 Floating Point Accuracy Issue (2) Check it out: float x(0.1); double y(0.1); cout << setprecision(20) << x << endl; cout << setprecision(20) << y << endl; You should also notice how the doubly precise representation for y gives a far better approximation of 0.1 than that of x, which is only represented by single precision! Regardless of how closely the number is to 0.1, it certainly is not equivalent

CSE202: Lecture 10AThe Ohio State University14 Floating Point Accuracy Issue (3) float x(0.1); double y(0.1); if (x == y) { cout << “x equals y” << endl; } The above cout statement will never execute because x and y are not equal (they’re VERY close, but not equal), and therefore the if- statement will not succeed!!

CSE202: Lecture 10AThe Ohio State University15 Be Consistent Always use the same data type for floating point variables. In C++, floating point expressions default to double. Consider the expression: cout << 4 * 9.34 << endl; The number 9.34 is not stored in a variable, but it still needs to be stored SOMEWHERE in memory. So what type is it? By default, C++ stores is as a double. This is why we ONLY use double (and not float or long double ) in this course!

CSE202: Lecture 10AThe Ohio State University16 Floating Point Accuracy Issue (3) double x(0.1); double y(0.1); if (x == y) { cout << “x equals y” << endl; } The above cout statement will execute since x equals y.

CSE202: Lecture 10AThe Ohio State University17 accuracyError.cpp // error caused by lack of accuracy #include using namespace std; int main() { double x(0.1); double y(1e-6); // y = 10^(-6) if (x == (1e5*y)) // if (x == 10^5*10^(-6)) { cout << x << " equals 1e5 x " << y << endl; } else { cout << x << " does not equal 1e5 x " << y << endl; } return 0; }

CSE202: Lecture 10AThe Ohio State University18 > accuracyError.exe 0.1 does not equal 1e5 x 1e-06 > … double x(0.1); double y(1e-6); // y = 10^(-6) if (x == (1e5*y)) // if (x == 10^5*10^(-6)) { cout << x << " equals 1e5 x " << y << endl; } else { cout << x << " does not equal 1e5 x " << y << endl; } …

CSE202: Lecture 10AThe Ohio State University19 Floating Point Accuracy Issue (4) Instead of checking: if (operand1 == operand2) See if the difference between them is small enough to assume truth: if (abs(operand1 - operand2) < )

CSE202: Lecture 10AThe Ohio State University20 Floating Point Accuracy Issue (5) Or, better yet: if (abs(operand1 – operand2) < EPSILON) 1.where EPSILON is some constant you have previously declared, and 2.abs() is the absolute value function for floating point numbers available by including the cmath library

CSE202: Lecture 10AThe Ohio State University21 accuracyExample.cpp // approximating numbers near zero #include using namespace std; int main() { double EPSILON(1e-12); double x(0.1); double y(1e-6); // y = 10^(-6) if (abs(x - (1e5*y)) < EPSILON) // if (abs(x-10^5*10^(-6)) < EPSILON) { cout << x << " equals 1e5 x " << y << endl; } else { cout << x << " does not equal 1e5 x " << y << endl; } return 0; }

CSE202: Lecture 10AThe Ohio State University22 > accuracyExample.exe 0.1 equals 1e5 x 1e-06 > double EPSILON (1e-12); double x(0.1); double y(1e-6); // y = 10^(-6) if (abs(x - (1e5*y)) < EPSILON) // if (abs(x-10^5*10^(-6)) < EPSILON) { cout << x << " equals 1e5 x " << y << endl; } else { cout << x << " does not equal 1e5 x " << y << endl; }

CSE202: Lecture 10AThe Ohio State University23 Type Casting

CSE202: Lecture 10AThe Ohio State University24 Type Casting (1) Review: We saw one form of coercion through assignment: int a, b; double c; c = a * b; a * b is an integer but the result is converted to a double when assigned to c. This type of coercion is implicit

CSE202: Lecture 10AThe Ohio State University25 Type Casting (2) There is another type of coercion, called “type casting”, which allows the programmer to explicitly force a data type onto an expression. The cast operator is: dataType(expression)

CSE202: Lecture 10AThe Ohio State University26 Type Casting (3) Example: int x = 5; double y = log(double(x)); Alternative format (C version): int x = 5; double y = log((double)(x));

CSE202: Lecture 10AThe Ohio State University27 logError.cpp // example of cmath function log #include using namespace std; int main() { int value(0); cout << "Enter value: "; cin >> value; // log(value) generates a compiler error cout << "At 10% interest, it will take " << log(value)/log(1.1) << " years for a $1 investment to be worth $" << value << "." << endl; return 0; }

CSE202: Lecture 10AThe Ohio State University28 > g++ logError.cpp –o logError.exe Compiling logError.cpp into logError.exe. logError.cpp: In function `int main()': logError.cpp:16: call of overloaded `log(int&)' is ambiguous /usr/include/iso/math_iso.h:52: candidates are: double log(double) /usr/local/include/g++-v3/bits/std_cmath.h:333: long double std::log(long double) /usr/local/include/g++-v3/bits/std_cmath.h:323: float std::log(float) … // log(value) generates a compiler error cout << "At 10% interest, it will take " << log(value)/log(1.1) << " years for a $1 investment to be worth $" << value << "." << endl; …

CSE202: Lecture 10AThe Ohio State University29 logTypeCast.cpp #include using namespace std; int main() { int value(0); cout << "Enter value: "; cin >> value; // type cast value to double cout << "At 10% interest, it will take " << log(double(value)) /log(1.1) << " years for a $1 investment to be worth $" << value << "." << endl; return 0; }

CSE202: Lecture 10AThe Ohio State University30 > logExample.exe Enter value: 10 At 10% interest, it will take years for a $1 investment to be worth $10. > … // type cast value to double cout << "At 10% interest, it will take " << log(double(value)) /log(1.1) << " years for a $1 investment to be worth $" << value << "." << endl; …

CSE202: Lecture 10AThe Ohio State University31 lower2Upper.cpp... int main() { int ascii_A(0), ascii_B(0); char a, b; cout << "Enter initials (2 char): "; cin >> a >> b ; cout << "Ascii " << a << ": " << int(a) << endl; cout << "Ascii " << b << ": " << int(b) << endl; ascii_A = int(a)-32; ascii_B = int(b)-32; cout << "Initials: "; cout << char(ascii_A) << char(ascii_B) << endl; return 0; }

ASCII Code CSE202: Lecture 10AThe Ohio State University32 CodeChar 32Space 33! 34" 35# 36$ 37% 38& 39' 40( 41) …… CodeChar …… CodeChar 65A 66B 67C 68D 69E 70F 71G 72H 73I 74J …… CodeChar 97a 98b 99c 100d 101e 102f 103g 104h 105i 106j ……

CSE202: Lecture 10AThe Ohio State University33 … cout << "Ascii " << a << ": " << int(a) << endl; cout << "Ascii " << b << ": " << int(b) << endl; ascii_A = int(a)-32; ascii_B = int(b)-32; cout << "Initials: "; cout << char(ascii_A) << char(ascii_B) << endl; … > lower2Upper.exe Enter initials (2 char): dj Ascii d: 100 Ascii j: 106 Initials: DJ >