Floating Point Numbers

Similar documents
Data Representation Floating Point

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

Data Representation Floating Point

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

Floating Point January 24, 2008

System Programming CISC 360. Floating Point September 16, 2008

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.

Giving credit where credit is due

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

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

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

Foundations of Computer Systems

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

Data Representation Floating Point

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

Floating Point Numbers

Floating Point Numbers

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

Computer Organization: A Programmer's Perspective

CS429: Computer Organization and Architecture

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

Representing and Manipulating Floating Points

Representing and Manipulating Floating Points

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

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

Representing and Manipulating Floating Points. Computer Systems Laboratory Sungkyunkwan University

Data Representa+on: Floa+ng Point Numbers

Representing and Manipulating Floating Points

Representing and Manipulating Floating Points. Jo, Heeseung

Floats are not Reals. BIL 220 Introduc6on to Systems Programming Spring Instructors: Aykut & Erkut Erdem

Floating Point. CENG331 - Computer Organization. Instructors: Murat Manguoglu (Section 1)

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

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

Up next. Midterm. Today s lecture. To follow

COMP2611: Computer Organization. Data Representation

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Roadmap. The Interface CSE351 Winter Frac3onal Binary Numbers. Today s Topics. Floa3ng- Point Numbers. What is ?

Floating Point Numbers

Systems Programming and Computer Architecture ( )

Ints and Floating Point

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

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

15213 Recitation 2: Floating Point

Finite arithmetic and error analysis

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

Floating Point Arithmetic

Floating Point Arithmetic

CO212 Lecture 10: Arithmetic & Logical Unit

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

Floating-point representations

Floating-point representations

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

Floating Point Numbers. Lecture 9 CAP

Numeric Encodings Prof. James L. Frankel Harvard University

CS367 Test 1 Review Guide

ECE232: Hardware Organization and Design

Introduction to Computer Systems Recitation 2 May 29, Marjorie Carlson Aditya Gupta Shailin Desai

3.5 Floating Point: Overview

Integers and Floating Point

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

Lecture 13: (Integer Multiplication and Division) FLOATING POINT NUMBERS

Numerical computing. How computers store real numbers and the problems that result

CS101 Introduction to computing Floating Point Numbers

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

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.

FLOATING POINT NUMBERS

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

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

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

Module 2: Computer Arithmetic

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


UC Berkeley CS61C : Machine Structures

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

Arithmetic for Computers. Hwansoo Han

Number Representations

CS61c Midterm Review (fa06) Number representation and Floating points From your friendly reader

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

95% of the folks out there are completely clueless about floating-point.

UCB CS61C : Machine Structures

IEEE Standard for Floating-Point Arithmetic: 754

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

Description Hex M E V smallest value > largest denormalized negative infinity number with hex representation 3BB0 ---

CS61C : Machine Structures

Floating Point Arithmetic. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Computer Arithmetic Ch 8

Computer Arithmetic Ch 8

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

Signed Multiplication Multiply the positives Negate result if signs of operand are different

Floating Point Arithmetic

Precision and Accuracy

Floating-point numbers. Phys 420/580 Lecture 6

Floating Point Representation in Computers

Bits and Bytes: Data Presentation Mohamed Zahran (aka Z)

Transcription:

Floating Point Floating Point Numbers Mathematical background: tional binary numbers Representation on computers: IEEE floating point standard Rounding, addition, multiplication Kai Shen 1 2 Fractional Binary Numbers bi bi 1 b2 b1 b0 b 1 b 2 b 3 b j Representation 1/2 1/4 1/8 2 j 2 i 2 i 1 4 2 1 Bits to right of binary point represent tional powers of 2 Represents rational number: What is 1011.101 2? Value Fractional Binary Numbers: Examples 5 & 3/4 101.112 2 & 7/8 010.1112 Representation 1 & 7/16 001.01112 Observations Each bit has half the weight of the immediately adjacent bit on the left Divide by 2 by shifting right; multiply by 2 by shifting left Numbers of form 0.111111 2 are just below 1.0 1/2 + 1/4 + 1/8 + + 1/2 i + 1.0 3 4 CSC252 - Spring 2015 1

Representable Numbers From Math to Computers Limitation Can only exactly represent numbers of the form x/2 k Other rational numbers have repeating bit representations Value Representation 1/3 0.0101010101[01] 2 1/5 0.001100110011[0011] 2 1/10 0.0001100110011[0011] 2 Representation of tional binary numbers on computers? Think about the limited number of bits we have Certain numbers bits before the binary point and certain numbers a er Fixed Point limited range Floating Point: numbers of bits before/after binary point may change (or floating binary point) higher range, but may compromise precision (unlike integers, there is no absolute precision anyway) 5 6 IEEE Floating Point Floating Point Representation IEEE Standard 754 Established in 1985 as uniform standard for floating point representation Before that, many idiosyncratic formats Supported by all major CPUs Driven by numerical concerns Nice standards for rounding, overflow, underflow Hard to make fast in hardware Numerical analysts predominated over hardware designers in defining standard Numerical form: ( 1) s M 2 E Sign bit s determines whether number is negative or positive Significand M normally a tional value in range [1.0,2.0). Exponent E weights value by power of two Encoding MSB s is sign bit s exp field encodes E (but is not equal to E) field encodes M (but is not equal to M) 7 8 CSC252 - Spring 2015 2

Precisions Normalized Values Single precision: 32 bits 1 8-bits 23-bits Double precision: 64 bits 1 11-bits 52-bits Extended precision: 80 bits (Intel only) 1 15-bits 63 or 64-bits 9 Condition: exp 000 0 and exp 111 1 Exponent coded as biased value: E = Exp Bias Exp: unsigned value exp Bias = 2 k 1 1, where k is number of exponent bits Single precision: 127 (Exp: 1 254, E: 126 127) Double precision: 1023 (Exp: 1 2046, E: 1022 1023) Significand coded with implied leading 1: M = 1.xxx x2 xxx x: bits of Minimum when 000 0 (M = 1.0) Maximum when 111 1 (M = 2.0 ε) Get extra leading bit for free 10 Normalized Encoding Example Denormalized Values float F = 15213.0; 15213 10 = 11101101101101 2 = 1.1101101101101 2 x 2 13 Significand M = 1.1101101101101 2 = 11011011011010000000000 2 Exponent E = 13 Bias = 127 Exp = 140 = 10001100 2 Result: 0 10001100 11011011011010000000000 Condition: exp = 000 0 Significand coded with implied leading 0: M = 0.xxx x2 Smooth transition to 0 Exponent value: E = 1 Bias (instead of E = 0 Bias) Same effective E as in the case of exp = 00 01 Smooth transition between largest value of exp = 00 00 and smallest value of exp = 00 01 Special case: exp = 000 0, = 000 0 Represents zero value (distinct +0 and 0) 11 12 CSC252 - Spring 2015 3

Special Values Tiny Floating Point Example Condition: exp = 111 1 Case: exp = 111 1, = 000 0 Represents value (infinity) Operation that overflows Both positive and negative E.g., 1.0/0.0 = 1.0/ 0.0 = +, 1.0/ 0.0 = Case: exp = 111 1, 000 0 Not a Number (NaN) Represents case when no numeric value can be determined E.g., sqrt( 1),, 0 1 4-bits 3-bits 8 bit floating point representation the sign bit is in the most significant bit the next four bits are the exponent, with a bias of 7 the last three bits are the Same general form as IEEE Format normalized, denormalized representation of 0, NaN, infinity 13 14 Dynamic Range (Positive Only) Interesting Numbers Denormalized numbers Normalized numbers E Value 0 0000 000-6 0 0 0000 001-6 1/8*1/64 = 1/512 0 0000 010-6 2/8*1/64 = 2/512 0 0000 110-6 6/8*1/64 = 6/512 0 0000 111-6 7/8*1/64 = 7/512 0 0001 000-6 8/8*1/64 = 8/512 0 0001 001-6 9/8*1/64 = 9/512 0 0110 110-1 14/8*1/2 = 14/16 0 0110 111-1 15/8*1/2 = 15/16 0 0111 000 0 8/8*1 = 1 0 0111 001 0 9/8*1 = 9/8 0 0111 010 0 10/8*1 = 10/8 0 1110 110 7 14/8*128 = 224 0 1110 111 7 15/8*128 = 240 0 1111 000 n/a inf closest to zero largest denorm smallest norm closest to 1 below closest to 1 above largest norm 15 Description exp Numeric Value Zero 00 00 00 00 0.0 Smallest Pos. Denorm. 00 00 00 01 2 {23,52} x 2 {126,1022} Single 1.4 x 10 45 Double 4.9 x 10 324 One 01 11 00 00 1.0 Largest Normalized 11 10 11 11 (2.0 ε) x 2 {127,1023} Single 3.4 x 10 38 Double 1.8 x 10 308 {single,double} 16 CSC252 - Spring 2015 4

Special Properties of Encoding Floating Point Operations: Basic Idea Floating point zero same as integer zero All bits = 0 Can (almost) use unsigned integer comparison Must first compare sign bits Must consider 0 = 0 NaNs problematic Will be greater than any other values Otherwise OK Denorm vs. normalized Normalized vs. infinity Additions, multiplications, Basic idea First compute exact result Make it fit into desired precision Possibly overflow if exponent too large Possibly round to fit into 17 18 Rounding Floating Point Addition Rounding modes 1.40 1.60 1.50 2.50 1.50 Towards zero 1 1 1 2 1 Round down ( ) 1 1 1 2 2 Round up (+ ) 2 2 2 3 1 Nearest even (default) 1 2 2 2 2 Nearest even Round to nearest acceptable point When exactly halfway between two adjacent points, round so that least significant digit is even Why? Statistically unbiased, even digit is easier to represent ( 1) s1 M1 2 E1 + ( 1) s2 M2 2 E2 Assume E1 > E2 Exact result: ( 1) s M 2 E Sign s, significand M: Result of signed align & add Exponent E: E1 ( 1) s M Fixing If M 2, shift M right, increment E if M < 1, shift M left k positions, decrement E by k Overflow if E out of range Round M to fit precision + ( 1) s1 M1 ( 1) s2 M2 E1 E2 19 20 CSC252 - Spring 2015 5

Properties of FP Add Floating Point Multiplication Commutative? Associative? Overflow and inexactness of rounding (1e20 + -1e20) + 3.14 = 3.14 1e20 + (-1e20 + 3.14) =?? Monotonicity: a b a+c b+c? Except for infinities & NaNs ( 1) s1 M1 2 E1 x ( 1) s2 M2 2 E2 Exact result: ( 1) s M 2 E Sign s: s1 ^ s2 Significand M: M1 x M2 Exponent E: E1 + E2 Fixing If M 2, shift M right, increment E If E out of range, overflow Round M to fit precision Implementation Biggest chore is multiplying significands 21 22 Mathematical Properties of FP Mult Floating Point in C Multiplication is commutative? Multiplication is associative? Possibility of overflow, inexactness of rounding Monotonicity: a b & c 0 a*c b*c? Except for infinities & NaNs 23 C guarantees two levels float single precision double double precision Conversions/casting Casting between int, float, and double changes bit representation double/float int Truncates tional part Like rounding toward zero Not defined when out of range or NaN: Generally sets to TMin int double Exact conversion, as long as int has 53 bit word size int float Will round according to rounding mode 24 CSC252 - Spring 2015 6

Disclaimer These slides were adapted from the CMU course slides provided along with the textbook of Computer Systems: A programmer s Perspective by Bryant and O Hallaron. The slides are intended for the sole purpose of teaching the computer organization course at the University of Rochester. 25 CSC252 - Spring 2015 7