CS 6210 Fall 2016 Bei Wang. Lecture 4 Floating Point Systems Continued

Similar documents
Floating Point Representation in Computers

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

Floating Point Arithmetic

Floating Point Numbers

Data Representation Floating Point

Floating Point Numbers

Floating Point Numbers

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

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

Data Representation Floating Point

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

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

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

ECE232: Hardware Organization and Design

Floating Point January 24, 2008

Floating Point. CSE 238/2038/2138: Systems Programming. Instructor: Fatma CORUT ERGİN. Slides adapted from Bryant & O Hallaron s slides

Floating Point Representation. CS Summer 2008 Jonathan Kaldor

COMP2611: Computer Organization. Data Representation

Foundations of Computer Systems

Giving credit where credit is due

Today: Floating Point. Floating Point. Fractional Binary Numbers. Fractional binary numbers. bi bi 1 b2 b1 b0 b 1 b 2 b 3 b j

Giving credit where credit is due

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

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

15213 Recitation 2: Floating Point

Representing and Manipulating Floating Points

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

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

CS 33. Data Representation (Part 3) CS33 Intro to Computer Systems VIII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

System Programming CISC 360. Floating Point September 16, 2008

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

Representing and Manipulating Floating Points

Floating Point Numbers

EE 109 Unit 19. IEEE 754 Floating Point Representation Floating Point Arithmetic

Divide: Paper & Pencil

Scientific Computing. Error Analysis

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

Representing and Manipulating Floating Points. Jo, Heeseung

Floating-point representations

Floating-point representations

Representing and Manipulating Floating Points

Data Representation Floating Point

CS429: Computer Organization and Architecture

Computer Architecture and IC Design Lab. Chapter 3 Part 2 Arithmetic for Computers Floating Point

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

Representing and Manipulating Floating Points. Computer Systems Laboratory Sungkyunkwan University

Inf2C - Computer Systems Lecture 2 Data Representation

Written Homework 3. Floating-Point Example (1/2)

Floating Point Numbers. Lecture 9 CAP

CO212 Lecture 10: Arithmetic & Logical Unit

CS101 Lecture 04: Binary Arithmetic

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Finite arithmetic and error analysis

Floating-point Arithmetic. where you sum up the integer to the left of the decimal point and the fraction to the right.

MAT128A: Numerical Analysis Lecture Two: Finite Precision Arithmetic

Up next. Midterm. Today s lecture. To follow

Floating-Point Numbers in Digital Computers

Computer Organization: A Programmer's Perspective

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Review Questions 26 CHAPTER 1. SCIENTIFIC COMPUTING

CS101 Introduction to computing Floating Point Numbers

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

Computer Systems C S Cynthia Lee

Classes of Real Numbers 1/2. The Real Line

Computer Arithmetic Floating Point

1.2 Round-off Errors and Computer Arithmetic

Computer Architecture Chapter 3. Fall 2005 Department of Computer Science Kent State University

3.5 Floating Point: Overview

Floating Point Arithmetic

Characters, Strings, and Floats

Floa.ng Point : Introduc;on to Computer Systems 4 th Lecture, Sep. 10, Instructors: Randal E. Bryant and David R.

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

Objectives. look at floating point representation in its basic form expose errors of a different form: rounding error highlight IEEE-754 standard

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

2 Computation with Floating-Point Numbers

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

The Sign consists of a single bit. If this bit is '1', then the number is negative. If this bit is '0', then the number is positive.

EE 109 Unit 20. IEEE 754 Floating Point Representation Floating Point Arithmetic

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

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Floating point numbers in Scilab

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

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

The course that gives CMU its Zip! Floating Point Arithmetic Feb 17, 2000

Chapter Three. Arithmetic

CS 61C: Great Ideas in Computer Architecture Performance and Floating Point Arithmetic

Floating-Point Numbers in Digital Computers

Floating Point. EE 109 Unit 20. Floating Point Representation. Fixed Point

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

CSE2003: System Programming (Spring 2010) Programming Assignment #1: Adding floating-point numbers. Due: April 11, 11:59PM

Real Numbers. range -- need to deal with larger range, not just [not just -2^(n-1) to +2^(n-1)-1] - large numbers - small numbers (fractions)

Floating-Point Arithmetic

Number Representation

Outline. What is Performance? Restating Performance Equation Time = Seconds. CPU Performance Factors

CS 61C: Great Ideas in Computer Architecture Performance and Floating-Point Arithmetic

ecture 25 Floating Point Friedland and Weaver Computer Science 61C Spring 2017 March 17th, 2017

Computer arithmetics: integers, binary floating-point, and decimal floating-point

Chapter 3: Arithmetic for Computers

Binary floating point encodings

Transcription:

CS 6210 Fall 2016 Bei Wang Lecture 4 Floating Point Systems Continued

Take home message 1. Floating point rounding 2. Rounding unit 3. 64 bit word: double precision (IEEE standard word) 4. Exact rounding 5. Guard digits 6. General floating point systems 7. Spacing of floating point numbers 8. Cancellation error 9. Good coding practice in floating point arithmetic

The almost complete story on FP rounding Decimal FP rounding (Integer rounding), Binary FP rounding Possibly all you have to know about FP rounding ON THE WHITEBOARD

Guard Digit ON THE WHITEBOARD

Rounding Unit ON THE WHITEBOARD

FP addition and multiplication Rules, Fixings and Properties ON THE WHITEBOARD

Good (bad) coding practices For FP arithmetic http://www.codeproject.com/articles/29637/five-tips-for-floating-point-programming

Rule 1: Don t test for equality Equality test likely to fail: FP numbers may not match in their last bits... Solution: set a tolerance...

Rule 2: FP has finite ranges DBL_EPSILON: smallest positive number e s.t. 1+e!=1. Here f = f+1 Because float (or double) has no precision left to represent f+1. Solution: be aware of data range and be ready to handle exception

Rule 3: Use Logarithms to avoid overflow and underflow How do you evaluate 200!/(190!10!)? Solution: consider log(xy) = logx + log y, then log(n!) = log(n) + log(n-1) + log(1)

Rule 4: Numerical operations do not always return numbers DBL_MAX: largest number to be represented by a double in C++ What to do when NaN occurs? Solution: exception handling.

Rule 5: SW specific rounding rules MATLAB: y = round(x): nearest integer; when tie, away from zero MATLAB: y=convergent(x): nearest integer; when tie, nearest even number Mathematica: y = Round[x]: nearest integer; when tie, nearest even number Python: round(x,n): round to the nearest; when tie, round away from zero Why not 2.68? Decimal to binary FP approximation with exact value closer to 2.67. https://docs.python.org/2/tutorial/floatingpoint.html

For your procrastination reading list 1. More details: http://www.codeproject.com/articles/25294/avoiding-overflow-un derflow-and-loss-of-precision 2. William D. (Bill) Young Lecture slides http://www.cs.utexas.edu/~byoung/cs429/slides4-fp.pdf

Extra Notes WHITEBOARD SUMMARY

The almost complete story on FP rounding Decimal FP rounding (Integer rounding), Binary FP rounding Possibly all you have to know about FP rounding WHITEBOARD SUMMARY

IEEE Standard for FP arithmetic (IEEE 754-1985, newer 2008) Five rounding rules (1-3 directed rounding, 4-5 round to nearest) 1. Round towards 0 truncation 2. Round towards +infinity round up 3. Round towards -infinity round down 4. Round to nearest, ties away from zero (typical for decimal FP) 5. Round to nearest, ties to even (default for binary FP, recommended for decimal FP): Round to the nearest value, if number falls exactly midway, it is rounded to the nearest value with an even (e.g. zero for binary FP) least significant bit. Rule 5 prevents upwards or downwards bias in summing many #s that look like x.5, since round up and round down happen 50% of the time.

Example 1. Decimal FP round to integer Rule 2.3 2.7 2.5-2.3-2.7-2.5 3.5-3.5 to 0 2 2 2-2 -2-2 3-3 to +infinity 3 3 3-2 -2-2 4-3 to -infinity nearest, tie away from 0 nearest, tie to even 2 2 2-3 -3-3 3-4 2 3 3-2 -3-3 4-4 2 3 2-2 -3-2 4-4

Example 2 decimal FP to nearest 100th (RULE 5) Decimal FP Rounded Value Rule 1.2349999 1.23 round down 1.2350001 1.24 round up 1.2350000 1.24 tie, round to even 1.2450000 1.24 tie, round to even

Example 3. Binary FP round to nearest ¼ (2 bits fraction) RULE 5 value Binary representation Rounded value in binary Rounded value in decimal Action 2 + 3/32 10.00011 10.00 2 round down 2 + 3/16 10.00110 10.01 2+1/4 round up 2+7/8 10.11100 11.00 3 tie to even (up) 2+5/8 10.10100 10.10 2+1/2 tie to even (down)

Guard Digit WHITEBOARD SUMMARY

Example: precision p = 3 Exact result: 10.1-9.93 = 0.17 Computing without guard digit x = 10.1 = 1.01 x 10^1, y = 0.99 x 10^1 x-y = 0.02 x 10^1 wrong! Computing with 1 guard digit x = 1.010 x 10^1, y = 0.993 x 10^1 x-y = 0.017 x 10^1 exact!

Theorems Theorem 1: Using FP format with parameters b (base) and p (precision), counting differences using p digits, the relative error of the result can be as large as b-1. Theorem 2: x and y are FP with base b and precision p, if subtraction is done with p+1 digits (i.e. 1 guard digit), then the relative error is upper bounded by 2 times machine precision. http://www.sci.utah.edu/~beiwang/teaching/cs6210/goldberg.pdf

Rounding Unit WHITEBOARD SUMMARY

Rounding Unit (a.k.a. machine precision) It is the upper bound on relative error due to rounding in FP arithmetic Base: b precision: p (number of digits of the significand, including the leading implicit bit) rounding unit: e = ½ b^{1-p} Example 1: b = 2, p = 53 (52 fraction bit, plus 1 implicit bit) e = ½ 2^{-52} = 2^{-53} 1.11 x e^{-16} Example 2 (consider single precision) NOTE: some authors refer to machine precision differently (differ by order of 2)

Deriving the Rounding Unit Hint: using two FP numbers that are close to each other, with the same exponent e, they have a spacing of b^{e-(p-1}}. Example: b = 2, p = 1, two adjacent FP numbers are 1x2^e and 0x2^e: spacing = 2^e b = 2, p = 2, 1.0x2^e, 1.1x2^e: spacing = 2^{e-1}

General FP System WHITEBOARD SUMMARY

General FP System IEEE Standard (b, p, L, U) b: base; p: precision; L <= e <= U Consider b = 2, common FP systems: name exponent bit fraction bit L U exponent bias binary16 half precision 5 11-14 15 2^4-1 = 15 binary32 single 8 24-126 127 2^7-1=127 binary64 double 11 53-1022 1023 2^10-1=1023 binary128 quadruple 15 113-16382 16383 2^14-1=16383

FP representation S: sign bit E = (exp bit - bias) M = 1.xx...x (p bits including p-1 bits of fraction) FP representation: (-1)^S x M x 2^E The largest number precisely represented by a Standard Word S = -1, E = 2013, M=1.11...1 (total 53 bits) (1+½+¼+...1/(2^52)) x 2^1023 = (2-2^{-52)} x 2^1023 2^1024 10^308 The smallest (1+0) x 2^(-1022) 2.2 x 10^(-308)

Credits Special thanks to all the people who made and released these awesome resources for free: Presentation template by SlidesCarnival Photographs by Unsplash