Presentation is loading. Please wait.

Presentation is loading. Please wait.

Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems

Similar presentations


Presentation on theme: "Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems"— Presentation transcript:

1 Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Autumn 2015 Week 3b: Floating Point Notation for Binary Fractions 13 October 2015

2 Binary Fractions 13 October 2015Birkbeck College, U. London2

3 Properties of Binary Fractions 1  Multiply by 2: move the radix point one place to the right, e.g. 1.01x2 = 10.1  Divide by 2: move the radix point one place to the left, e.g. 1.02÷2 = 0.101 13 October 2015Birkbeck College, U. London3

4 Properties of Binary Fractions 2  A number can be specified exactly by a binary fraction if and only if it has the form integer/power of 2  E.g. 1.01 specifies the decimal fraction 5/4 0.0101010101010101…. specifies 1/3 13 October 2015Birkbeck College, U. London4

5 Brookshear, Section 1.75 Specification of a Binary Fraction -101.11001 The binary fraction has three parts: The sign – The position of the radix point The bit string 10111001 13 October 2015

6 Brookshear, Section 1.76 Binary Fraction and Powers of 2 13 October 2015 1 01. 1 1 0 0 1 22 22 2121 20 20 2 -1 2 -2 2 -3 2 -4 2 -5 2 2 +2 0 +2 -1 +2 -2 +2 -5 = 5+(25/32)

7 Brookshear, Section 1.77 Reconstruction of a Binary Fraction The sign is + The position of the radix point is just to the right of the second bit from the left The bit string is 101101 What is the binary fraction? 13 October 2015

8 Brookshear, Section 1.78 Summary To represent a binary fraction three pieces of information are needed: Sign Position of the radix point Bit string

9 Spacing Between Numbers 13 October 2015Birkbeck College, U. London9 Two’s complement: equally spaced numbers 0 Floating point: big gaps between big numbers, small gaps between small numbers. 0

10 The Key: Exponents 13 October 2015Birkbeck College, U. London10 2 -4 2323 2 -2 2 -3 2 -1 20202 2121 1/16 1/8 ¼ ½ 1 2 4 8 -4 -3 -2 -1 0 1 2 3 big gaps between big numbers small gaps between small numbers

11 Brookshear, Section 1.711 Standard Form for a Binary Fraction  Any non-zero binary fraction can be written in the form ±2 r x 0.t where t is a bit string beginning with 1.  Examples 11.001 = +2 2 x 0.11001 -0.011011 = -2 -1 x 0.11011 13 October 2015

12 Brookshear, Section 1.712 Floating Point Representation Write a non-zero binary fraction in the form ± 2 r x 0.t Record the sign – bit string s1 Record r – bit string s2 Record t – bit string s3 Output s1||s2||s3 13 October 2015

13 Brookshear, Section 1.713 Floating Point Notation 8 bit floating point: se1e2e3m1m2m3m4 sign exponent mantissa 1 bit 3 bits 4 bits radix r bit string t The exponent is in 3 bit excess notation

14 13 October 2015Brookshear, Section 1.714 To Find the Floating Point Notation Write the non-zero number as ± 2 r x 0.t If sign = -1, then s1=1, else s1=0. s2 = 3 bit excess notation for r. s3= leftmost four bits of t.

15 13 October 2015Birkbeck College, U. London15 Example b= - 0.00101011101 s=1 b= -2 -2 x 0.101011101 exponent = -2, s2 =010 Floating point notation 10101010

16 13 October 2015Birkbeck College, U. London16 Second Example Floating point notation: 10111100 s1=1, therefore negative. s2 = 011, exponent=-1 s3 = 1100 Binary fraction -0.011 = -3/8

17 Birkbeck College, U. London17 Class Examples Find the floating point representation of the decimal number -1 1/8 Find the decimal number which has the floating point representation 01101101 13 October 2015

18 Brookshear, Section 1.718 Round-Off Error 2+5/8= 10.101 2 ½ = 10.100 The 8 bit floating point notations for 2 5/8 and 2 ½ are the same: 01101010 The error in approximating 2+5/8 with 10.100 is round-off error or truncation error.

19 Floating Point Addition of Numbers x, y a = floating point number nearest to x b = floating point number nearest to y c=a+b z=floating point number nearest to c z=x@y 13 October 2015Birkbeck College, U. London19

20 13 October 2015Birkbeck College, U. London20 Examples of Floating Point Addition 2 ½: 01101010 1/8: 00101000 ¼: 00111000 2 ¾: 01101011 2 ½ @(1/8 @ 1/8)=2 ½ @ 1/4=2 ¾ (2 ½ @ 1/8) @ 1/8=2 ½ @ 1/8=2 ½

21 13 October 2015Birkbeck College, U. London21 Round-Off in Decimal and Binary 1/5=0.2 exactly in decimal notation 1/5=0.0011001100110011….. in binary notation 1/5 cannot be represented exactly in binary floating point no matter how many bits are used. Round-off is unavoidable but it is reduced by using more bits.

22 Birkbeck College, U. London22 Floating Point Errors Overflow: number too large to be represented. Underflow: number <>0 and too small to be represented. Invalid operation: e.g. SquareRoot[-1]. See http://en.wikipedia.org/wiki/Floating_point 13 October 2015

23 Birkbeck College, U. London23 IEEE Standard for Floating Point Arithmetic For a general discussion of fp arithmetic see http://www.ee.columbia.edu/~marios/matlab/Fall96Cleve.pdf 01…89…31 Sign s bit 0 Exponent e bits 1-8 Mantissa m bits 9-31 If 0<e<255, then value = (-1) s x 2 e-127 x 1.m If e=0, s=0, m=0, then value = 0 If e=0, s=1, m=0, then value = -0 Single precision, 32 bits. 13 October 2015

24 Numbers in Computing 13 October 2015Birkbeck College, U. London24 q = 0.1 The value stored in the memory location q is not 0.1! E.g. in Python the value stored is 0.1000000000000000055511151231257827021181583404541015625 See https://docs.python.org/2/tutorial/floatingpoint.html


Download ppt "Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems"

Similar presentations


Ads by Google