introduction to computer systemssjmaybank/ics/ics week 3b.pdf · 2020-01-22 · birkbeck college,...
TRANSCRIPT
![Page 1: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/1.jpg)
Birkbeck College, U. London 1
Introduction to Computer Systems
Department of Computer Science and Information Systems
Lecturer: Steve Maybank
Spring 2020
Week 3b: Floating Point Notation for Binary Fractions
28 January 2020
![Page 2: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/2.jpg)
Binary Fractions
A binary fraction has the form
sign||bitString1||radix point||bitString2
E.g. +1.01, -10011.11
The + is usually omitted
Digits to the right of the radix point specify powers of 2 with negative exponents
E.g. 1.01 is20 + 0 × 2−1 + 2−2 = 1 + 1/4
Birkbeck College, U. London 2
![Page 3: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/3.jpg)
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.01÷2 = 0.101
Birkbeck College, U. London 3
![Page 4: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/4.jpg)
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
Examples
1.01 specifies the decimal fraction 5/4
An infinite number of bits is required to specify 1/3: 0.0101010101010101….
Birkbeck College, U. London 4
![Page 5: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/5.jpg)
Brookshear, Section 1.7 5
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
![Page 6: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/6.jpg)
Brookshear, Section 1.7 6
Binary Fraction and Powers of 2
1 0 1 . 1 1 0 0 1
22 21 20 2-1 2-2 2-3 2-4 2-5
22+20+2-1+2-2+2-5 = 5+(25/32)
![Page 7: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/7.jpg)
Brookshear, Section 1.7 7
Reconstruction of a Binary Fraction
The sign is +
The radix point is between the second bit from the left and the third bit from the left
The bit string is 101101
What is the binary fraction?
![Page 8: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/8.jpg)
Brookshear, Section 1.7 8
Summary
To represent a binary fraction three pieces of information are needed:
Sign
Position of the radix point
Bit string
![Page 9: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/9.jpg)
Spacing Between Numbers
Birkbeck College, U. London 9
Two’s complement:equally spacednumbers
0
Floating point:big gaps between big numberssmall gaps between numbers near to 0
0
![Page 10: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/10.jpg)
The Key: Exponents
Birkbeck College, U. London 10
2-4 232-22-3 2-1 20 2221
1/16 1/8 ¼ ½ 1 2 4 8
-4 -3 -2 -1 0 1 2 3
big gaps between big numberssmall gaps between numbers close to 0
![Page 11: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/11.jpg)
Brookshear, Section 1.7 11
Standard Form for a Binary Fraction
Any non-zero binary fraction can be written in the form
±2r x 0.t
where t is a bit string beginning with 1
Examples
11.001 = +22 x 0.11001
-0.011011 = -2-1 x 0.11011
![Page 12: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/12.jpg)
Brookshear, Section 1.7 12
Floating Point Representation
Write a non-zero binary fraction in the form ± 2r x 0.t
Record the sign – bit string s1
Record r – bit string s2
Record t – bit string s3
Output s1||s2||s3
![Page 13: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/13.jpg)
Brookshear, Section 1.7 13
Floating Point Notation
8 bit floating point:
s e1 e2 e3 m1 m2 m3 m4
sign exponent mantissa1 bit 3 bits 4 bits
radix r bit string t
The exponent is in 3 bit excess notation
![Page 14: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/14.jpg)
Brookshear, Section 1.7 14
To Find the Floating Point Notation
Write the non-zero number as ± 2r 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
![Page 15: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/15.jpg)
Birkbeck College, U. London 15
Example
b= - 0.00101011101
s=1
b= -2-2 x 0.101011101
exponent = -2, s2 =010
Floating point notation
10101010
![Page 16: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/16.jpg)
Birkbeck College, U. London 16
Second Example
Floating point notation: 10111100
s1 = 1, therefore negative.
s2 = 011, exponent=-1
s3 = 1100
Binary fraction -0.011 = -3/8
![Page 17: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/17.jpg)
Birkbeck College, U. London 17
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
![Page 18: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/18.jpg)
Brookshear, Section 1.7 18
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.
![Page 19: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/19.jpg)
Floating Point Addition of Numbers x, y
a = floating point number nearest to x
b = floating point number nearest to y
c = a+b
result = floating point number nearest to c
Birkbeck College, U. London 19
![Page 20: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/20.jpg)
Birkbeck College, U. London 20
Examples of Floating Point Addition
2 ½: 01101010
1/8: 00101000
¼: 00111000
2 ¾: 01101011
2 ½ +(1/8 + 1/8) = 2 ½ + ¼ = 2 ¾
(2 ½ + 1/8) + 1/8 = 2 ½ + 1/8 = 2 ½
![Page 21: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/21.jpg)
Birkbeck College, U. London 21
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
![Page 22: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/22.jpg)
Birkbeck College, U. London 22
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]
https://en.wikipedia.org/wiki/Floating-point_arithmetic
![Page 23: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/23.jpg)
Birkbeck College, U. London 23
IEEE Standard for Floating Point Arithmetic
See http://steve.hollasch.net/cgindex/coding/ieeefloat.html
sign
1 bit
exponent
8 bits
mantissa
23 bits
If 0<e<255, then value = (-1)s x 2e-127 x 1.mIf e=0, s=0, m=0, then value = 0If e=0, s=1, m=0, then value = -0
Single precision, 32 bits:
![Page 24: Introduction to Computer Systemssjmaybank/ICS/ICS week 3b.pdf · 2020-01-22 · Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information](https://reader035.vdocuments.us/reader035/viewer/2022081400/5f1bbee91f4bfe70806051a8/html5/thumbnails/24.jpg)
Numbers in Computing
Birkbeck College, U. London 24
q = 0.1
The value stored in the memory location q may not be 0.1!
E.g. in Python the value stored is
0.1000000000000000055511151231257827021181583404541015625
See https://docs.python.org/2/tutorial/floatingpoint.html