Representing and Manipulating Floating Points

Similar documents
Representing and Manipulating Floating Points

Representing and Manipulating Floating Points

Representing and Manipulating Floating Points. Computer Systems Laboratory Sungkyunkwan University

Representing and Manipulating Floating Points. Jo, Heeseung

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

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

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

Computer Organization: A Programmer's Perspective

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

Giving credit where credit is due

Giving credit where credit is due

Floating Point January 24, 2008

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

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

System Programming CISC 360. Floating Point September 16, 2008

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

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

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

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

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

Floating Point Numbers

Data Representation Floating Point

CS429: Computer Organization and Architecture

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

Foundations of Computer Systems

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

Data Representation Floating Point

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

Floating Point Numbers

Floating Point Numbers

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

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

Up next. Midterm. Today s lecture. To follow

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

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

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

Data Representa+on: Floa+ng Point Numbers

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

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Arithmetic for Computers. Hwansoo Han

ECE232: Hardware Organization and Design

Ints and Floating Point

Systems Programming and Computer Architecture ( )

Floating Point Numbers

COMP2611: Computer Organization. Data Representation

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

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

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

Floating Point Arithmetic

CO212 Lecture 10: Arithmetic & Logical Unit

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

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

15213 Recitation 2: Floating Point

Floating Point Arithmetic

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.

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

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

Floating-point representations

Floating-point representations

Floating Point Arithmetic

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

Finite arithmetic and error analysis

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

Floating Point Numbers. Lecture 9 CAP

Numeric Encodings Prof. James L. Frankel Harvard University

Integers and Floating Point

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

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

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

Inf2C - Computer Systems Lecture 2 Data Representation

FLOATING POINT NUMBERS

Computer Arithmetic Floating Point

3.5 Floating Point: Overview

Manipulating Integers

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

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

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

Number Representations

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

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

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

Divide: Paper & Pencil

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)

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

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

Integer Representation Floating point Representation Other data types

Floating-point representation

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

CS 101: Computer Programming and Utilization

Module 2: Computer Arithmetic

Scientific Computing. Error Analysis

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

Floating Point. CSC207 Fall 2017

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

CS101 Introduction to computing Floating Point Numbers


CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

The ALU consists of combinational logic. Processes all data in the CPU. ALL von Neuman machines have an ALU loop.

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

Transcription:

Representing and Manipulating Floating Points Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu)

The Problem How to represent fractional values with finite number of bits?.1.612 3.1415926535897932384626433832795288... Wide ranges of numbers 1 Light-Year = 9,46,73,472,58.8 km The radius of a hydrogen atom:.25 m SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 2 2

Fractional Binary Numbers (1) Representation Bits to right of binary point represent fractional powers of 2 i Represents rational number: å b k 2 k 2 i 2 i 1 k=- j 4 2 1 b i b i 1 b 2 b 1 b. b 1 b 2 b 3 b j 1/2 1/4 1/8 2 j SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 3 3

Fractional Binary Numbers (2) Examples: Value Representation 5 3/4 11.11 2 2 7/8 1.111 2 63/64.111111 2 Observations Divide by 2 by shifting right Multiply by 2 by shifting left Numbers of form.111111.. 2 just below 1. 1/2 + 1/4 + 1/8 + + 1/2 i + à 1. Use notation 1. e SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 4 4

Fractional Binary Numbers (3) Representable numbers Can only exactly represent numbers of the form x / 2 k Other numbers have repeating bit representations Value Representation 1/3.11111[1] 2 1/5.111111[11] 2 1/1.111111[11] 2 SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 5 5

Fixed-Point Representation (1) p.q Fixed-point representation Use the rightmost q bits of an integer as representing a fraction Example: 17.14 fixed-point representation 1 bit for sign bit 17 bits for the integer part 14 bits for the fractional part An integer x represents the real number x / 2 14 Maximum value: (2 31 1) / 2 14 13171.999 Integer part (2 2 +2 1 =5) Fractional part (2-1 +2-2 +2-4 =.8125) 1 1 1 1 1 Sign bit Imaginary binary point SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 6 6

Fixed-Point Representation (2) Properties Convert n to fixed point: n * f (= n << q) Add x and y: x + y + 1 1 1 1 1 1 1 13.5 1.75 1 1 1 1 1 15.25 Subtract y from x: x y Add x and n: x + n * f Multiply x by n: x * n Divide x by n: x / n x, y: fixed-point number n: integer f = 1 << q SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 7 7

Fixed-Point Representation (3) Pros Simple Can use integer arithmetic to manipulate No floating-point hardware needed Used in many low-cost embedded processors or DSPs (digital signal processors) Cons Cannot represent wide ranges of numbers SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 8 8

Representing Floating Points IEEE standard 754 Established in 1985 as uniform standard for floating-point arithmetic Before that, many idiosyncratic formats Supported by all major CPUs William Kahan, a primary architect of IEEE 754, won the Turing Award in 1989 Driven by numerical concerns Nice standards for rounding, overflow, underflow Hard to make go fast Numerical analysts predominated over hardware types in defining standard SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 9 9

FP Representation Numerical form: -1 s x M x 2 E Sign bit s determines whether number is negative or positive Significand M normally a fractional value in range [1., 2.) Exponent E weights value by power of two Encoding s exp frac MSB is sign bit s exp field encodes E (Exponent) frac field encodes M (Mantissa) SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 11

FP Precisions s exp frac Single precision 8 exp bits, 23 frac bits (32 bits total) Double precision 11 exp bits, 52 frac bits (64 bits total) Extended precision 15 exp bits, 63 frac bits Only found in Intel-compatible machines Stored in 8 bits (1 bit wasted) SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 1111

Normalized Values Condition: exp and exp 111 1 Exponent coded as a biased value E = Exp Bias Exp: unsigned value denoted by exp Bias: Bias value (=2 k-1-1, where k is the number of exp bits) Single precision (k=8): 127 (Exp: 1..254, E: -126..127) Double precision (k=11): 123 (Exp: 1..246, E: -122..123) Significand coded with implied leading 1 M = 1.xxx x 2 Minimum when frac = (M = 1.) Maximum when frac = 111 1 (M = 2. e) Get extra leading bit for free SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 1212

Normalized Values: Example float f = 23.; 23 1 = 11111111 2 = 1.1111111 2 X 2 1 Significand M = 1.1111111 2 frac = 1111111 2 Exponent E = 1 Exp = E + Bias = 1 + 127 = 137 = 111 2 Hex: 4 4 F A 6 Binary: 1 1 1111 11 11 137: 1 1 1 23: 1111 11 11 SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 1313

Denormalized Values Condition: exp = Value Exponent value E = 1 Bias Significand value M =.xxx x 2 (no implied leading 1) Case 1: exp =, frac = Represents value. Note that there are distinct values + and - Case 2: exp =, frac Numbers very close to. Gradual underflow : possible numeric values are spaced evenly near. SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 1414

Special Values Condition: exp = 111 1 Case 1: exp = 111 1, frac = Represents value (infinity) Operation that overflows Both positive and negative e.g. 1./. = 1./. = +, 1./. = Case 2: exp = 111 1, frac Not-a-Number (NaN) Represents case when no numeric value can be determined e.g. sqrt( 1), -, *, SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 1515

Tiny FP Example (1) 8-bit floating point representation The sign bit is in the most significant bit The next four bits are the exp, with a bias of 7 The last three bits are the frac Same general form as IEEE format Normalized, denormalized Representation of, NaN, infinity 7 6 s exp frac 3 2 SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 1616

Tiny FP Example (2) Values related to the exponent (Bias = 7) Description Exp exp E = Exp - Bias 2 E Denormalized -6 1/64 1 2 3 4 5 6 1 1 11 1 11 11-6 -5-4 -3-2 -1 1/64 1/32 1/16 1/8 1/4 1/2 Normalized 7 111 1 8 1 1 2 9 1 11 12 13 14 11 11 111 11 111 111 2 3 4 5 6 7 4 8 16 32 64 128 inf, NaN 15 1111 - - SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 1717

Tiny FP Example (3) Dynamic range Description Bit representation e E f M V Zero -6 Smallest pos. 1-6 1/8 1/8 1/512 1-6 2/8 2/8 2/512 11-6 3/8 3/8 3/512 11-6 6/8 6/8 6/512 Largest denorm. 111-6 7/8 7/8 7/512 Smallest norm. 1 1-6 8/8 8/512 1 1 1-6 1/8 9/8 9/512 11 11 6-1 6/8 14/8 14/16 11 111 6-1 7/8 15/8 15/16 One 111 7 8/8 1 111 1 7 1/8 9/8 9/8 111 1 7 2/8 1/8 1/8 111 11 14 7 6/8 14/8 224 Largest norm. 111 111 14 7 7/8 15/8 24 Infinity 1111 - - - - + SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 1818

Tiny FP Example (4) Encoded values (nonnegative numbers only) 111 XXX = (8/8 ~ 15/8)*2 6 111 XXX = (8/8 ~ 15/8)*2 7 16 32 48 64 8 96 112 128 144 16 176 192 28 224 24 256 + 111 XXX = (8/8 ~ 15/8)*2 1 XXX = (8/8 ~ 15/8)*2 1..5 1. 1.5 2. 2.5 3. 3.5 4. XXX = (/8 ~ 7/8)*2-6 1 XXX = (8/8 ~ 15/8)*2-6 11 XXX = (8/8 ~ 15/8)*2-4..2.4.6.8.1.12 (Without denormalization)..2.4.6.8.1.12 XXX = (8/8 ~ 15/8)*2-7 SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 1919

Interesting Numbers Normalized Denorm +Denorm +Normalized + NaN - + NaN Description exp frac Numeric Value Zero. Smallest Positive denormalized Largest Denormalized Smallest Positive Normalized 1 111 11 1 Single: 2-23 X 2-126 1.4 X 1-45 Double: 2-52 X 2-122 4.9 X 1-324 Single: (1. ε) X 2-126 1.18 X 1-38 Double: (1. ε) X 2-122 2.2 X 1-38 Single: 1. X 2-126, Double: 1. X 2-122 (Just larger than largest denormalized) One 11 11 1. Largest Normalized 111 1 111 11 Single: (2. ε) X 2 127 3.4 X 1 38 Double: (2. ε) X 2 123 1.8 X 1 38 SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 22

Special Properties FP zero same as integer zero All bits = Can (almost) use unsigned integer comparison Must first compare sign bits Must consider = NaNs problematic Will be greater than any other values Otherwise OK Denormalized vs. normalized Normalized vs. Infinity SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 2121

Rounding For a given value x, finding the closest matching value x that can be represented in the FP format IEEE 754 defines four rounding modes Round-to-even avoids statistical bias by rounding upward or downward so that the least significant digit is even Rounding modes $1.4 $1.6 $1.5 $2.5 $ 1.5 Round-toward-zero $1 $1 $1 $2 $ 1 Round-down ( ) $1 $1 $1 $2 $ 2 Round-up (+ ) $2 $2 $2 $3 $ 1 Round-to-even (default) or Round-to-nearest $1 $2 $2 $2 $ 2 SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 2222

Round-to-Even Round up conditions R = 1, S = 1 à >.5 G = 1, R = 1, S = à Round to even 1.BBGRXXX Guard bit: LSB of result Round bit: 1 st bit removed Stickybit: OR of remaining bits Value Fraction GRS Up? Rounded 128 1. (x2 7 ) No 1. 13 1.11 (x2 3 ) 1 No 1.11 17 1.1 (x2 4 ) 1 No 1. 19 1.11 (x2 4 ) 11 Yes 1.1 138 1.11 (x2 7 ) 11 Yes 1.1 63 1.11111 (x2 5 ) 111 Yes 1. SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 2323

FP Addition Adding two numbers: (Assume E1 > E2) Align binary points Shift right M2 by E1 E2 Add significands + Result: Sign s, Significand M, Exponent E (= E1) Normalize result if (M 2), shift M right, increment E s1 E1 M1 + s2 E2 M2 ( 1) s1 M1 if (M < 1), shift M left k positions, decrement E by k Check for overflow (E out of range?) Round M and renormalize if necessary ( 1) s M E1 E2 ( 1) s2 M2 SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 2424

FP Multiplication Multiplying two numbers: Obtain exact result Sign s = s1 ^ s2 Significand M = M1 x M2 Exponent E = E1 + E2 The biggest chore is multiplying significands Normalize result s1 E1 M1 x s2 E2 M2 if (M 2), shift M right, increment E if (M < 1), shift M left k positions, decrement E by k Check for overflow (E out of range?) Round M and renormalize if necessary SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 2525

Floating Points in C C guarantees two levels float (single precision) vs. double (double precision) Conversions double or float à int Truncates fractional 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 SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 2626

FP Example 1 #include <stdio.h> int main () { int n = 123456789; int nf, ng; float f; double g; } f = (float) n; g = (double) n; nf = (int) f; ng = (int) g; printf ( nf=%d ng=%d\n, nf, ng); SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 2727

FP Example 2 #include <stdio.h> int main () { double d; d = 1. +.1 +.1 +.1 +.1 +.1 +.1 +.1 +.1 +.1 +.1; } printf ( d = %.2f\n, d); SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 2828

FP Example 3 #include <stdio.h> int main () { float f1 = (3.14 + 1e2) 1e2; float f2 = 3.14 + (1e2 1e2); } printf ( f1 = %f, f2 = %f\n, f1, f2); SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 2929

Ariane 5 Ariane 5 tragedy (June 4, 1996) Exploded 37 seconds after liftoff Satellites worth $5 million Why? Computed horizontal velocity as floatingpoint number Converted to 16-bit integer Careful analysis of Ariane 4 trajectory proved 16-bit is enough Reused a module from 1-year-old software Overflowed for Ariane 5 No precise specification for the software SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 33

Summary IEEE floating point has clear mathematical properties Represents numbers of form M x 2 E Can reason about operations independent of implementation As if computed with perfect precision and then rounded Not the same as real arithmetic Violates associativity / distributivity Makes life difficult for compilers and serious numerical applications programmers SSE23: Introduction to Computer Systems, Spring 218, Jinkyu Jeong (jinkyu@skku.edu) 3131