Independent Representation

Similar documents
Floating Point Arithmetic

Roundoff Errors and Computer Arithmetic

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Floating Point. The World is Not Just Integers. Programming languages support numbers with fraction

Floating Point Representation in Computers

CHAPTER 3 Expressions, Functions, Output

2.1.1 Fixed-Point (or Integer) Arithmetic

Finite arithmetic and error analysis

2 Computation with Floating-Point Numbers

MATH 353 Engineering mathematics III

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Computer System and programming in C

Data Representation Floating Point

1.2 Round-off Errors and Computer Arithmetic

Data Representation Floating Point

Floating Point (with contributions from Dr. Bin Ren, William & Mary Computer Science)

Review of Calculus, cont d

Floating Point Puzzles The course that gives CMU its Zip! Floating Point Jan 22, IEEE Floating Point. Fractional Binary Numbers.

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011


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

Programming in C++ 5. Integral data types

Floating Point Numbers

Floating Point Numbers

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

Floating-point numbers. Phys 420/580 Lecture 6

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Up next. Midterm. Today s lecture. To follow

Lab 6. Review of Variables, Formatting & Loops By: Dr. John Abraham, Professor, UTPA

These are reserved words of the C language. For example int, float, if, else, for, while etc.

2 Computation with Floating-Point Numbers

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

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

Module 2: Computer Arithmetic

Chapter 3. Errors and numerical stability

CS321 Introduction To Numerical Methods

Floating-Point Numbers in Digital Computers

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Floating-Point Numbers in Digital Computers

Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

Chapter 4. Operations on Data

The Perils of Floating Point

Computer Arithmetic Floating Point

Scientific Computing. Error Analysis

CMPS 221 Sample Final

Representing and Manipulating Floating Points

Floating-point representation

Foundations of Computer Systems

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Systems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties

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

Floating Point January 24, 2008

Floating Point : Introduction to Computer Systems 4 th Lecture, May 25, Instructor: Brian Railing. Carnegie Mellon

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Floating-point representations

Floating-point representations

CS101 Lecture 04: Binary Arithmetic

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Representing and Manipulating Floating Points

Floating Point Representation. CS Summer 2008 Jonathan Kaldor

Floating Point Considerations

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

1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions.

System Programming CISC 360. Floating Point September 16, 2008

Lecture 17 Floating point

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Number Representation 3/2/01 Lecture #

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

Floating-Point Arithmetic

3.5 Floating Point: Overview

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

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

Computational Economics and Finance

Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1

Divide: Paper & Pencil

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Representing and Manipulating Floating Points. Jo, Heeseung

Representing and Manipulating Floating Points. Computer Systems Laboratory Sungkyunkwan University

Representing and Manipulating Floating Points

C NUMERIC FORMATS. Overview. IEEE Single-Precision Floating-point Data Format. Figure C-0. Table C-0. Listing C-0.

CMPSCI 145 MIDTERM #2 Solution Key SPRING 2018 April 13, 2018 Professor William T. Verts

C++ PROGRAMMING SKILLS Part 4: Arrays

Number Systems. Both numbers are positive

Data Representation Floating Point

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Giving credit where credit is due

Giving credit where credit is due

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

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

UNIT-III COMPUTER ARTHIMETIC

Mathematical preliminaries and error analysis

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS Arithmetic (a) The four possible cases Carry (b) Truth table x y

Split up Syllabus (Session )

Transcription:

Independent Representation 1 Integer magnitude + 1 2 3 4 5 6 7 8 9 0 Float sign sign of exponent mantissa On standard 32 bit machines: INT_MAX = 2147483647 which gives 10 digits of precision, (i.e. the maximum number of significant digits). Range is the interval from the smallest representable value to the largest representable value. + + 0 1 2 3 4 5 6 7 8 sign of exponent mantissa Real numbers are stored in a normalized format, (the first digit of the mantissa is nonzero).

Arithmetic Errors 2 Storage The representation to the right corresponds to the real number(s): sign of exponent mantissa 2.345678 2.3456789 2.34567891 The last two, not represented exactly due to limited precision, are examples of representational, (truncation), error. Some machines round instead of truncate and would thus represent the last 2 numbers as shown at the right. The difference between the actual value and the rounded value introduces rounding error. + + 0 1 2 3 4 5 6 7 8 sign of exponent mantissa + + 0 1 2 3 4 5 6 7 9

Arithmetic Errors (cont) 3 Operations The addition, 0.9999999 x 10 99 + 1, results in an overflow error, an attempt to represent a number larger than the maximum representable value. Analogously, underflow is an attempt to represent a number smaller than the minimum representable value. Integer overflow & underflow occurs when an expression evaluates to a value outside the range [ INT_MIN... INT_MAX ]. The C++ reference manual states that the result of overflow is undefined, (i.e. compiler dependent). The manual states that the result of underflow is also undefined, (compiler dependent).

Arithmetic Errors (cont) 4 Operations Consider the simple problem of summing real numbers stored in a file one per line, (example given at right). The result after the first three additions would be 3000000.0. The second & fourth number having no effect on the result due to limited precision & the large differences in the numbers, termed cancellation error. 1000000.0 1000000.0 0.000123 0.000123 2000000.0 2000000.0 0.000456 0.000456 In the worst case, if the entire file was organized in this manner only half of the numbers would be added. Solution: sort the numbers & add the smallest first so they may affect the total.

Considerations 5 Comparisons Due to representational errors, (and conversion errors), float values should never be compared for equality. Looping For the same reasons float variables should not be used for loop control. Dangerous // loop from 0.0 to 1.0 in increments of 0.1 float realvar = 0.0 ; while ( realvar < 1.0 ) { // realvar = realvar + 0.1 ; Float Float Equality Check Check abs abs (( real1 real1 --real2 real2 )) < epsilon epsilon where where epsilon epsilon = 0.00001 0.00001 an an appropriate value value near near the the precision of of the the machine. Better // loop from 0.0 to 1.0 in increments of 0.1 i = 0 ; while ( i < 10 ) { realvar = i / 10.0 ; // i = i + 1 ;

Floats as LCVs 6 The short program below illustrates the perils of direct comparison of float values. Logically the variable X should take values 0.000, 0.001, 0.002,..., 0.999, 1.000. However, when executed on a Pentium II cpu, the program produces output as shown below. #include <iostream.h> #include <iomanip.h> void main() { float X = 0.0f; float DeltaX = 0.001f; int Counter = 0; cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); 993 0.99299 994 0.99399 995 0.99499 996 0.99599 997 0.99699 998 0.99799 999 0.99899 1000 0.99999 1001 1.00099 while (X < 1.0) { cout << setw( 4) << Counter << setw(10) << setprecision(5) << X << endl; X = X + DeltaX; Counter++; cout << setw( 4) << Counter << setw(10) << setprecision(5) << X << endl;

Float Underflow 7 The short program below illustrates a float variable underflowing to zero. Logically the while loop should be infinite. However, when executed on a Pentium II cpu, the program produces output as shown below. #include <iostream.h> #include <iomanip.h> void main() { const float One = 1.0f; float X = 1.0f; int Counter = 1; cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); while (X!= 0.0f) { cout << setw( 3) << Counter << setw(15) << setprecision(6) << X << endl; X = X / 2.0f; Counter++; 1 1.000000 2 0.500000 3 0.250000 4 0.125000 5 0.062500 6 0.031250 7 0.015625 8 0.007813 9 0.003906 10 0.001953 11 0.000977 12 0.000488 13 0.000244 14 0.000122 15 0.000061 16 0.000031 17 0.000015 18 0.000008 19 0.000004 20 0.000002 21 0.000001 22 0.000000... 149 0.000000 150 0.000000

Machine Epsilon 8 The short program below illustrates a float variable underflowing to zero. Logically the while loop should be infinite. However, when executed on a Pentium II cpu, the program produces output as shown below. #include <iostream.h> #include <iomanip.h> void main() { const float One = 1.0f; float X = 1.0f; int Counter = 1; cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); while (One + X!= One) { cout << setw( 3) << Counter << setw(15) << setprecision(6) << X << endl; X = X / 2.0f; Counter++; 1 1.000000 2 0.500000 3 0.250000 4 0.125000 5 0.062500 6 0.031250 7 0.015625 8 0.007813 9 0.003906 10 0.001953 11 0.000977 12 0.000488 13 0.000244 14 0.000122 15 0.000061 16 0.000031 17 0.000015 18 0.000008 19 0.000004 20 0.000002 21 0.000001 22 0.000000... 52 0.000000 53 0.000000