CSE1301 Computer Programming Lecture 33: Real Number Representation

Slides:



Advertisements
Similar presentations
Spring 2013 Advising Starts this week! CS2710 Computer Organization1.
Advertisements

Computer Engineering FloatingPoint page 1 Floating Point Number system corresponding to the decimal notation 1,837 * 10 significand exponent A great number.
Topics covered: Floating point arithmetic CSE243: Introduction to Computer Architecture and Hardware/Software Interface.
Floating Point Numbers
Faculty of Computer Science © 2006 CMPUT 229 Floating Point Representation Operating with Real Numbers.
COMP3221: Microprocessors and Embedded Systems Lecture 14: Floating Point Numbers Lecturer: Hui Wu Session 2, 2004.
CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (2)
Computer ArchitectureFall 2007 © September 5, 2007 Karem Sakallah CS 447 – Computer Architecture.
1 CSE1301 Computer Programming Lecture 30: Real Number Representation.
Dr Damian Conway Room 132 Building 26
Chapter 5 Floating Point Numbers. Real Numbers l Floating point representation is used whenever the number to be represented is outside the range of integer.
CSE 378 Floating-point1 How to represent real numbers In decimal scientific notation –sign –fraction –base (i.e., 10) to some power Most of the time, usual.
1 Error Analysis Part 1 The Basics. 2 Key Concepts Analytical vs. numerical Methods Representation of floating-point numbers Concept of significant digits.
Floating Point Numbers
Computer ArchitectureFall 2008 © August 27, CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)
Simple Data Type Representation and conversion of numbers
Binary Real Numbers. Introduction Computers must be able to represent real numbers (numbers w/ fractions) Two different ways:  Fixed-point  Floating-point.
Information Representation (Level ISA3) Floating point numbers.
Computer Organization and Architecture Computer Arithmetic Chapter 9.
Computer Arithmetic Nizamettin AYDIN
1 Lecture 5 Floating Point Numbers ITEC 1000 “Introduction to Information Technology”
CEN 316 Computer Organization and Design Computer Arithmetic Floating Point Dr. Mansour AL Zuair.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Information Representation: Negative and Floating Point.
Fixed-Point Arithmetics: Part II
Computing Systems Basic arithmetic for computers.
ECE232: Hardware Organization and Design
Floating Point. Agenda  History  Basic Terms  General representation of floating point  Constructing a simple floating point representation  Floating.
Data Representation in Computer Systems
S. Rawat I.I.T. Kanpur. Floating-point representation IEEE numbers are stored using a kind of scientific notation. ± mantissa * 2 exponent We can represent.
Computer Science Engineering B.E.(4 th sem) c omputer system organization Topic-Floating and decimal arithmetic S ubmitted to– Prof. Shweta Agrawal Submitted.
9.4 FLOATING-POINT REPRESENTATION
Round-off Errors and Computer Arithmetic. The arithmetic performed by a calculator or computer is different from the arithmetic in algebra and calculus.
1 Lecture 2  Complement  Floating Point Number  Character Encoding.
CSC 221 Computer Organization and Assembly Language
ITEC 1011 Introduction to Information Technologies 4. Floating Point Numbers Chapt. 5.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Information Representation: Floating Point Representation.
1 Number Systems Lecture 10 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Lecture notes Reading: Section 3.4, 3.5, 3.6 Multiplication
1 Floating Point Operations - Part II. Multiplication Do unsigned multiplication on the mantissas including the hidden bits Do unsigned multiplication.
CSPP58001 Floating Point Numbers. CSPP58001 Floating vs. fixed point Floating point refers to a binary decimal representation where there is not a fixed.
Computer Architecture Lecture 22 Fasih ur Rehman.
Computer Engineering FloatingPoint page 1 Floating Point Number system corresponding to the decimal notation 1,837 * 10 significand exponent A great number.
Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
Floating Point Numbers Representation, Operations, and Accuracy CS223 Digital Design.
Binary Arithmetic.
Floating Point Numbers
COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.7 Instructor: Lin Chen Sept 2013.
Lecture 6: Floating Point Number Representation Information Representation: Floating Point Number Representation Lecture # 7.
Module 2.2 Errors 03/08/2011. Sources of errors Data errors Modeling Implementation errors Absolute and relative errors Round off errors Overflow and.
Cosc 2150: Computer Organization Chapter 9, Part 3 Floating point numbers.
Introduction to Numerical Analysis I
Floating Point Representations
Fundamentals of Computer Science
Introduction To Computer Science
Lecture 9: Floating Point
Floating Point Number system corresponding to the decimal notation
IEEE floating point format
A Level Computing Component 2
PRESENTED BY J.SARAVANAN. Introduction: Objective: To provide hardware support for floating point arithmetic. To understand how to represent floating.
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
How to represent real numbers
How to represent real numbers
Approximations and Round-Off Errors Chapter 3
COMS 161 Introduction to Computing
Lecture 9: Shift, Mult, Div Fixed & Floating Point
Presentation transcript:

CSE1301 Computer Programming Lecture 33: Real Number Representation

Topics Terminology IEEE standard for floating-point representation Floating point arithmetic Limitations

Some Terminology All digits in a number following any leading zeros are significant digits: 12.345 -0.12345 0.00012345

Some Terminology (cont) Lecture 34: Numerical 2 Some Terminology (cont) The scientific notation for real numbers is: mantissa  base exponent In C, the expression: 12.456e-2 means: 12.456  10-2

Some Terminology (cont) The mantissa is always normalized between 1 and the base (i.e., exactly one significant digit before the point) Unnormalized Normalized 2997.9  105 2.9979  108 B1.39FC  1611 B.139FC  1612 0.010110110101  2-1 1.0110110101  2-3

Some Terminology (cont) Lecture 34: Numerical 2 Some Terminology (cont) The precision of a number is how many digits (or bits) we use to represent it For example: 3 3.14 3.1415926 3.1415926535897932384626433832795028

Lecture 34: Numerical 2 Representing Numbers A real number n is represented by a floating-point approximation n* The computer uses 32 bits (or more) to store each approximation It needs to store the mantissa the sign of the mantissa the exponent (with its sign)

Representing Numbers (cont) The standard way to allocate 32 bits (specified by IEEE Standard 754) is: 23 bits for the mantissa 1 bit for the mantissa's sign 8 bits for the exponent

Representing Numbers (cont) 23 bits for the mantissa 1 bit for the mantissa's sign 8 bits for the exponent

Representing Numbers (cont) 23 bits for the mantissa 1 bit for the mantissa's sign 8 bits for the exponent

Representing Numbers (cont) 23 bits for the mantissa 1 bit for the mantissa's sign 8 bits for the exponent

Representing the Mantissa The mantissa has to be in the range 1  mantissa < base Therefore If we use base 2, the digit before the point must be a 1 So we don't have to worry about storing it We get 24 bits of precision using 23 bits

Representing the Mantissa (cont) Lecture 34: Numerical 2 Representing the Mantissa (cont) 24 bits of precision are equivalent to a little over 7 decimal digits:

Representing the Mantissa (cont) Lecture 34: Numerical 2 Representing the Mantissa (cont) Suppose we want to represent : 3.1415926535897932384626433832795..... That means that we can only represent it as: 3.141592 (if we truncate) 3.141593 (if we round)

Representing the Mantissa (cont) Lecture 34: Numerical 2 Representing the Mantissa (cont) Even if the computer appears to represent more than 7 decimal places, only the first 7 places are meaningful For example: #include <math.h> main() { float pi = 2 * asin(1); printf("%.35f\n", pi); } Prints out: 3.1415927419125732000000000000000000 3.1415927419125732000000000000000000

Representing the Exponent Lecture 34: Numerical 2 Representing the Exponent The exponent is represented as excess-127. E.g., Actual Exponent Stored Value -127  00000000 -126  00000001 . . . 0  01111111 +1  10000000 i  (i+127)2 +128  11111111

Representing the Exponent (cont) Lecture 34: Numerical 2 Representing the Exponent (cont) The IEEE standard restricts exponents to the range: –126  exponent  +127 The exponents –127 and +128 have special meanings: If exponent = -127, the stored value is 0 If exponent = 128, the stored value is 

Representing Numbers -- Example 1 What is 01011011 (8-bit machine) ? Lecture 34: Numerical 2 Representing Numbers -- Example 1 What is 01011011 (8-bit machine) ? 0 101 1011 sign exp mantissa Mantissa: 1.1011 Exponent (excess-3 format): 5-3=2 1.1011  22  110.11 110.112 = 22 + 21 + 2-1 + 2-2 = 4 + 2 + 0.5 + 0.25 = 6.75

Representing Numbers -- Example 2 Represent -10.375 (32-bit machine) Lecture 34: Numerical 2 Representing Numbers -- Example 2 Represent -10.375 (32-bit machine) 10.37510 = 10 + 0.25 + 0.125 = 23 + 21 + 2-2 + 2-3 = 1010.0112  1.0100112  23 Sign: 1 Mantissa: 010011 Exponent (excess-127 format): 3+127 = 13010 = 100000102 1 10000010 01001100000000000000000

Floating Point Overflow Lecture 34: Numerical 2 Floating Point Overflow Floating point representations can overflow, e.g., 1.111111  2127 + 1.111111  2127                 11.111110  2127 =  1.1111110  2128

Floating Point Underflow Lecture 34: Numerical 2 Floating Point Underflow Floating point numbers can also get too small, e.g., 10.010000  2-126 ÷ 11.000000  20                 0.110000  2-126 = 0 1.100000  2-127

Floating Point Addition Lecture 34: Numerical 2 Floating Point Addition Five steps to add two floating point numbers: Express the numbers with the same exponent (denormalize) Add the mantissas Adjust the mantissa to one digit/bit before the point (renormalize) Round or truncate to required precision Check for overflow/underflow

Lecture 34: Numerical 2 Floating Point Addition -- Example 1 (Assume precision 4 decimal digits) x = 9.876  107 y = 1.357  106

Lecture 34: Numerical 2 Floating Point Addition -- Example 1 (cont) (Assume precision 4 decimal digits) 1. Use the same exponents: x = 9.876  107 y = 0.1357  107

Lecture 34: Numerical 2 Floating Point Addition -- Example 1 (cont) (Assume precision 4 decimal digits) 2. Add the mantissas: x = 9.876  107 y = 0.136  107 x+y = 10.012  107

Floating Point Addition -- Example 1 (cont) (Assume precision 4 decimal digits) 3. Renormalize the sum: x = 9.876  107 y = 0.136  107 x+y = 1.0012  108

Lecture 34: Numerical 2 Floating Point Addition -- Example 1 (cont) (Assume precision 4 decimal digits) 4. Truncate or round: x = 9.876  107 y = 0.136  107 x+y = 1.001  108

Lecture 34: Numerical 2 Floating Point Addition -- Example 1 (cont) (Assume precision 4 decimal digits) 5. Check overflow and underflow: x = 9.876  107 y = 0.136  107 x+y = 1.001  108

Floating Point Addition -- Example 2 (Assume precision 4 decimal digits) y = -3.497  10-5

Floating Point Addition -- Example 2 (cont) (Assume precision 4 decimal digits) 1. Use the same exponents: x = 3.506  10-5 y = -3.497  10-5

Floating Point Addition -- Example 2 (cont) (Assume precision 4 decimal digits) 2. Add the mantissas: x = 3.506  10-5 y = -3.497  10-5 x+y = 0.009  10-5

Lecture 34: Numerical 2 Floating Point Addition -- Example 2 (cont) (Assume precision 4 decimal digits) 3. Renormalize the sum: x = 3.506  10-5 y = -3.497  10-5 x+y = 9.000  10-8

Floating Point Addition -- Example 2 (cont) (Assume precision 4 decimal digits) 4. Truncate or round: x = 3.506  10-5 y = -3.497  10-5 x+y = 9.000  10-8 (no change)

Floating Point Addition -- Example 2 (cont) (Assume precision 4 decimal digits) 5. Check overflow and underflow: x = 3.506  10-5 y = -3.497  10-5 x+y = 9.000  10-8

Floating Point Multiplication Lecture 34: Numerical 2 Floating Point Multiplication Five steps to multiply two floating point numbers: Multiply the mantissas Add the exponents Renormalize the mantissa Round or truncate to required precision Check for overflow/underflow

Floating Point Multiplication -- Example (Assume precision 4 decimal digits) y = 8.001  10-3

Lecture 34: Numerical 2 Floating Point Multiplication -- Example (cont) (Assume precision 4 decimal digits) 1&2. Multiply mantissas and Add exponents: x = 9.001  105 y = 8.001  10-3 x  y = 72.017001  102

Floating Point Multiplication -- Example (cont) (Assume precision 4 decimal digits) 3. Renormalize the mantissa: x = 9.001  105 y = 8.001  10-3 x  y = 7.2017001  103

Floating Point Multiplication -- Example (cont) (Assume precision 4 decimal digits) 4. Truncate or round: x = 9.001  105 y = 8.001  10-3 x  y = 7.201  103

Floating Point Multiplication -- Example (cont) (Assume precision 4 decimal digits) 4. Truncate or round: x = 9.001  105 y = 8.001  10-3 x  y = 7.202  103

Floating Point Multiplication -- Example (cont) (Assume precision 4 decimal digits) 5. Check overflow and underflow: x = 9.001  105 y = 8.001  10-3 x  y = 7.202  103

Lecture 34: Numerical 2 Limitations Floating-point representations only approximate real numbers The normal laws of arithmetic don't always hold, e.g., associativity is not guaranteed

Limitations -- Example (Assume precision 4 decimal digits) y = -3.000  103 z = 6.531  100

Limitations -- Example (cont) (Assume precision 4 decimal digits) y = -3.000  103 z = 6.531  100 x+y = 2.000  100

Limitations -- Example (cont) (Assume precision 4 decimal digits) x+y = 2.000  100 y = -3.000  103 z = 6.531  100 (x+y)+z = 8.531  100

Limitations -- Example (cont) (Assume precision 4 decimal digits) y = -3.000  103 z = 6.531  100

Limitations -- Example (cont) (Assume precision 4 decimal digits) Lecture 34: Numerical 2 Limitations -- Example (cont) (Assume precision 4 decimal digits) x = 3.002  103 y = -3.000  103 z = 6.531  100 y+z = -2.993  103

Limitations -- Example (cont) (Assume precision 4 decimal digits) y = -3.000  103 y+z = -2.993  103 z = 6.531  100 x+(y+z) = 0.009  103

Limitations -- Example (cont) (Assume precision 4 decimal digits) x+(y+z) = 9.000  100 y = -3.000  103 y+z = -2.993  103 z = 6.531  100

Limitations -- Example (cont) (Assume precision 4 decimal digits) x+(y+z) = 9.000  100 y = -3.000  103 (x+y)+z = 8.531  100 z = 6.531  100

Limitations -- Exercise Laws of Arithmetic Lecture 34: Numerical 2 Limitations -- Exercise Laws of Arithmetic Consider the laws of arithmetic: Commutativity (additive and multiplicative) Associativity Distributivity Identity (additive and multiplicative) Try to work out which ones always hold for floating-point numbers

Reading (for the Very Keen) Goldberg, D., What Every Computer Scientist Should Know About Floating-Point Arithmetic, ACM Computing Surveys, Vol.23, No.1, March 1991 Knuth, D.E., The Art of Computer Programming (Vol 2) -- Seminumerical Algorithms, Section 4.4, pp. 319-329 (ed 3)