1 University of Washington Fractional binary numbers What is 1011.101?

Slides:



Advertisements
Similar presentations
Fixed Point Numbers The binary integer arithmetic you are used to is known by the more general term of Fixed Point arithmetic. Fixed Point means that we.
Advertisements

Fabián E. Bustamante, Spring 2007 Floating point Today IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties Next time.
– 1 – CS213, S’06 Floating Point 4/5/2006 Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties CS213.
Floating Point Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties CS 367.
Carnegie Mellon Instructors: Dave O’Hallaron, Greg Ganger, and Greg Kesden Floating Point : Introduction to Computer Systems 4 th Lecture, Sep 6,
1 Binghamton University Floating Point CS220 Computer Systems II 3rd Lecture.
Float’s and reals CENG331: Introduction to Computer Systems 4 th Lecture Instructor: Erol Sahin Acknowledgement: Most of the slides are adapted from the.
Carnegie Mellon Instructors: Randy Bryant & Dave O’Hallaron Floating Point : Introduction to Computer Systems 3 rd Lecture, Aug. 31, 2010.
University of Washington Today: Floats! 1. University of Washington Today Topics: Floating Point Background: Fractional binary numbers IEEE floating point.
University of Washington Today Topics: Floating Point Background: Fractional binary numbers IEEE floating point standard: Definition Example and properties.
Floating Point Arithmetic August 25, 2007
Floating Point Numbers
Floating Point Arithmetic February 15, 2001 Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties IA32 floating.
FLOATING POINT COMPUTER ARCHITECTURE AND ORGANIZATION.
Floating Point Sept 6, 2006 Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties class03.ppt “The course.
Computing Systems Basic arithmetic for computers.
Floating Point Numbers Topics –IEEE Floating Point Standard –Rounding –Floating Point Operations –Mathematical properties.
Floating Point. Agenda  History  Basic Terms  General representation of floating point  Constructing a simple floating point representation  Floating.
Floating Point (a brief look) We need a way to represent –numbers with fractions, e.g., –very small numbers, e.g., –very large numbers,
Carnegie Mellon Instructors: Seth Copen Goldstein, Anthony Rowe, Greg Kesden Floating Point : Introduction to Computer Systems 4 th Lecture, Jan.
Instructor: Andrew Case Floating Point Slides adapted from Randy Bryant & Dave O’Hallaron.
Carnegie Mellon Instructors: Greg Ganger, Greg Kesden, and Dave O’Hallaron Floating Point : Introduction to Computer Systems 4 th Lecture, Sep 4,
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Instructor: San Skulrattanakulchai Floating Point MCS-284:
HW 3 Due 3/9 (Mon) 11:00 AM Probs. 2.81, 2.87, 2.88.
CSC 221 Computer Organization and Assembly Language
Computer Arithmetic Floating Point. We need a way to represent –numbers with fractions, e.g., –very small numbers, e.g., –very large.
Today How’s Lab 1 going? Some of these are tricky (for us too)
Floating Point CENG331 - Computer Organization Instructors:
Carnegie Mellon Instructors: Randy Bryant, Dave O’Hallaron, and Greg Kesden Floating Point : Introduction to Computer Systems 4 th Lecture, Sep 5,
Floating Point Arithmetic Feb 17, 2000 Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties IA32 floating point.
Floating Point Sept 9, 2004 Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties class04.ppt “The course.
CS 232: Computer Architecture II Prof. Laxmikant (Sanjay) Kale Floating point arithmetic.
University of Washington Floating-Point Numbers The Hardware/Software Interface CSE351 Winter 2013.
1 Floating Point. 2 Topics Fractional Binary Numbers IEEE 754 Standard Rounding Mode FP Operations Floating Point in C Suggested Reading: 2.4.
Floating Point Carnegie Mellon /18-243: Introduction to Computer Systems 4 th Lecture, 26 May 2011 Instructors: Gregory Kesden.
Lecture 4 IEEE 754 Floating Point Topics IEEE 754 standard Tiny float January 25, 2016 CSCE 212 Computer Architecture.
Floating Point Topics IEEE Floating-Point Standard Rounding Floating-Point Operations Mathematical Properties CS 105 “Tour of the Black Holes of Computing!”
Floating Point Arithmetic Sept. 24, 1998 Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties Alpha floating.
Floating Point Representations
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Today’s Instructor: Randy Bryant Floating Point :
Cosc 2150: Computer Organization Chapter 9, Part 3 Floating point numbers.
Floating Point Representations
Lecture 6. Fixed and Floating Point Numbers
Data analysis and modeling: the tools of the trade
Floating Point CSE 238/2038/2138: Systems Programming
Floating Point Borrowed from the Carnegie Mellon (Thanks, guys!)
Floating Point Numbers
Floats! Coursecast on! Today:
CS 105 “Tour of the Black Holes of Computing!”
2.4. Floating Point Numbers
Topics IEEE Floating Point Standard Rounding Floating Point Operations
CS 105 “Tour of the Black Holes of Computing!”
Floating Point Number system corresponding to the decimal notation
CS 367 Floating Point Topics (Ch 2.4) IEEE Floating Point Standard
Floating Point.
Topic 3d Representation of Real Numbers
CS201 - Lecture 5 Floating Point
CS 105 “Tour of the Black Holes of Computing!”
How to represent real numbers
CS 105 “Tour of the Black Holes of Computing!”
Floating Point Arithmetic August 31, 2009
CS 105 “Tour of the Black Holes of Computing!”
CS 105 “Tour of the Black Holes of Computing!”
CS213 Floating Point Topics IEEE Floating Point Standard Rounding
Topic 3d Representation of Real Numbers
CS 105 “Tour of the Black Holes of Computing!”
CS 105 “Tour of the Black Holes of Computing!”
Presentation transcript:

1 University of Washington Fractional binary numbers What is ?

2 University of Washington b –1. Fractional Binary Numbers Representation  Bits to right of “binary point” represent fractional powers of 2  Represents rational number: bibi b i–1 b2b2 b1b1 b0b0 b –2 b –3 b–jb–j i–1 2i2i 1/2 1/4 1/8 2–j2–j

3 University of Washington Fractional Binary Numbers: Examples ValueRepresentation 5 and 3/4 2 and 7/8 63/64 Observations  Divide by 2 by shifting right  Multiply by 2 by shifting left  Numbers of form … 2 are just below 1.0  1/2 + 1/4 + 1/8 + … + 1/2 i + … → 1.0  Use notation 1.0 – 

4 University of Washington Representable Numbers Limitation  Can only exactly represent numbers of the form x/2 k  Other rational numbers have repeating bit representations ValueRepresentation 1/ [01]… 2 1/ [0011]… 2 1/ [0011]… 2

University of Washington Fixed Point Representation float → 32 bits; double → 64 bits We might try representing fractional binary numbers by picking a fixed place for an implied binary point “fixed point binary numbers” Let's do that, using 8 bit floating point numbers as an example #1: the binary point is between bits 2 and 3 b 7 b 6 b 5 b 4 b 3 [.] b 2 b 1 b 0 #2: the binary point is between bits 4 and 5 b 7 b 6 b 5 [.] b 4 b 3 b 2 b 1 b 0 The position of the binary point affects the range and precision − range: difference between the largest and smallest representable numbers − precision: smallest possible difference between any two numbers

University of Washington Fixed Point Pros and Cons Pros It's simple. The same hardware that does integer arithmetic can do fixed point arithmetic − In fact, the programmer can use ints with an implicit fixed point E.g., int balance; // number of pennies in the account − ints are just fixed point numbers with the binary point to the right of b 0 Cons There is no good way to pick where the fixed point should be − Sometimes you need range, sometimes you need precision. The more you have of one, the less of the other

7 University of Washington IEEE Floating Point Fixing fixed point: analogous to scientific notation  Not but 1.2 x 10^7; not but 1.2 x 10^-6 IEEE Standard 754  Established in 1985 as uniform standard for floating point arithmetic  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

8 University of Washington Numerical Form: (–1) s M 2 E Sign bit s determines whether number is negative or positive Significand (mantissa) M normally a fractional value in range [1.0,2.0). Exponent E weights value by power of two Encoding MSB s is sign bit s frac field encodes M (but is not equal to M) exp field encodes E (but is not equal to E) Floating Point Representation sexpfrac

9 University of Washington Precisions Single precision: 32 bits Double precision: 64 bits Extended precision: 80 bits (Intel only) sexpfracsexpfracsexpfrac or 64

10 University of Washington Normalization and Special Values “Normalized” means mantissa has form 1.xxxxx x 2 5 and 1.1 x 2 3 represent the same number, but the latter makes better use of the available bits Since we know the mantissa starts with a 1, don't bother to store it How do we do 0? How about 1.0/0.0?

11 University of Washington Normalization and Special Values “Normalized” means mantissa has form 1.xxxxx x 2 5 and 1.1 x 2 3 represent the same number, but the latter makes better use of the available bits Since we know the mantissa starts with a 1, don't bother to store it Special values: The float value represents zero If the exp == and the mantissa == , it represents ∞ E.g., 10.0 / 0.0 → ∞ If the exp == and the mantissa != , it represents NaN “Not a Number” Results from operations with undefined result − E.g., 0 * ∞

12 University of Washington How do we do operations? Is representation exact? How are the operations carried out?

13 University of Washington Floating Point Operations: Basic Idea x + f y = Round(x + y) x * f y = Round(x * y) Basic idea  First compute exact result  Make it fit into desired precision  Possibly overflow if exponent too large  Possibly round to fit into frac

14 University of Washington Floating Point Multiplication (–1) s1 M1 2 E1 * (–1) s2 M2 2 E2 Exact Result: (–1) s M 2 E  Sign s: s1 ^ s2  Significand M: M1 * M2  Exponent E: E1 + E2 Fixing  If M ≥ 2, shift M right, increment E  If E out of range, overflow  Round M to fit frac precision Implementation  What is hardest?

15 University of Washington Floating Point Addition (–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 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 frac precision (–1) s1 M1 (–1) s2 M2 E1–E2 + (–1) s M

16 University of Washington Hmm… if we round at every operation…

17 University of Washington Mathematical Properties of FP Operations Not really associative or distributive due to rounding Infinities and NaNs cause issues Overflow and infinity

18 University of Washington Floating Point in C C Guarantees Two Levels floatsingle precision doubledouble precision Conversions/Casting  Casting between int, float, and double changes bit representation  Double/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, why?  int → float  Will round according to rounding mode

19 University of Washington Memory Referencing Bug double fun(int i) { volatile double d[1] = {3.14}; volatile long int a[2]; a[i] = ; /* Possibly out of bounds */ return d[0]; } fun(0) –>3.14 fun(1) –>3.14 fun(2) –> fun(3) –> fun(4) –>3.14, then segmentation fault Saved State d7 … d4 d3 … d0 a[1] a[0] Location accessed by fun(i)

University of Washington Floating Point and the Programmer #include int main(int argc, char* argv[]) { float f1 = 1.0; float f2 = 0.0; int i; for ( i=0; i<10; i++ ) { f2 += 1.0/10.0; } printf("0x%08x 0x%08x\n", *(int*)&f1, *(int*)&f2); printf("f1 = %10.8f\n", f1); printf("f2 = %10.8f\n\n", f2); f1 = 1E30; f2 = 1E-30; float f3 = f1 + f2; printf ("f1 == f3? %s\n", f1 == f3 ? "yes" : "no" ); return 0; }

University of Washington Floating Point and the Programmer #include int main(int argc, char* argv[]) { float f1 = 1.0; float f2 = 0.0; int i; for ( i=0; i<10; i++ ) { f2 += 1.0/10.0; } printf("0x%08x 0x%08x\n", *(int*)&f1, *(int*)&f2); printf("f1 = %10.8f\n", f1); printf("f2 = %10.8f\n\n", f2); f1 = 1E30; f2 = 1E-30; float f3 = f1 + f2; printf ("f1 == f3? %s\n", f1 == f3 ? "yes" : "no" ); return 0; } $./a.out 0x3f x3f f1 = f2 = f1 == f3? yes

22 University of Washington Summary As with integers, floats suffer from the fixed number of bits available to represent them Can get overflow/underflow, just like ints Some “simple fractions” have no exact representation E.g., 0.1 Can also lose precision, unlike ints “Every operation gets a slightly wrong result” Mathematically equivalent ways of writing an expression may compute differing results NEVER test floating point values for equality!