CH03 Data II
CH03 Data II
Data Representation II
COMP1411: Introduction to Computer Systems
Acknowledgement: These slides are based on the textbook (Computer Systems: A Programmer’s Perspective) and its slides.
These slides are only intended to use internally. Do not publish it anywhere without permission.
Overview
• Representing fractional numbers with binary
• IEEE floating point standard
• Examples and properties
• Computation over floating point numbers
Rethinking fractional decimal numbers
425.367
• Limitation on precision
• Given fixed number of digits in the fractional
part, precision is fixed. In the above Option Max Precision
example, 3 digits in the fractional part
means we can only represent a unit as small X.XXXXX 9.99999 0.00001
as 0.001 --- precision limitation XX.XXXX 99.9999 0.0001
• If we want arbitrary precision, we need an XXX.XXX 999.999 0.001
infinite number of digits
XXXX.XX 9999.99 0.01
• Limitation of representations XXXXX.X 99999.9 0.1
• Given 6 digits in total to represent a
(fractional) number
Fractional binary numbers
• Fractional binary numbers
1011.1012
4 = 22
••• 2 = 21
1 = 20
bi-
bi ••• b2 -1b1 b0 b-1 b-2 b-3 ••• b-j
1 2 = 1/2
2-2 = 1/4 •••
2-3 = 1/8
2-j
Precision limit: 1 / 2j
Representable numbers
• Limitation #1
• Can only exactly represent numbers of the form x/2k (k: fractional digits)
• Values that cannot be exactly represented
• 1/3 0.0101010101[01]…2
• 1/5 0.001100110011[0011]…2
• 1/10 0.0001100110011[0011]…2
• Limitation #2
• Given a fixed number of bits, limited range of numbers
• More integer bits, or more fraction bits?
• Larger value, or better precision?
• We are running out of bits …
s exp frac
v = (–1)s M 2E
• Any binary fractional number can be written into the following
numerical form
• Sign bit s determines whether number is negative or positive
• Significand M is normally a fractional value in range [1.0,2.0).
• Exponent E weights value by power of two
• Decimal value v
Normalized values - Definition
s exp frac v = (–1)s M 2E
0 10001100 11011011011010000000000
• k=8 v = (–1)s M 2E
• S=0
• Significand
frac = 110110110110100000000002
M = 1.11011011011012
• Exponent
V = (-1)0 * 1.11011011011012 * 213
Exp = 100011002 = 140
= 111011011011012
Bias = 27 – 1 = 127
= 1521310
E = Exp – Bias = 13
Denormalized values - Definition
s 0000000…0 frac v = (–1)s M 2E
NaN NaN
0 +0
• The IEEE 754 FP encoding covers non-negative values in range [0, 240], with 120 available
bit vectors representing 120 different values (including both integer and fractional values).
• However, the 120 values represented are not evenly distributed in range [0, 240].
• We obtain the distribution of the values and find out that this encoding devotes more bit
vectors to represent small values while less bit vectors to represent large values. For
example, 50+ values are in range [0, 1], while only 1 value falls in range [225, 240].
• On the small values’ side, this encoding can achieve higher precision; while towards the
large values’ side, precision is decreased and on the contrary, larger values are covered.
• The design idea is that people may need higher precision when they deal with small
values; while for large values, the precision seems not as critical.
• This is a trade-off between precision and the breadth of the value range. This encoding is
clever, if the design idea reflects common usage patterns. This is the case in reality.
Decimal value IEEE FP 754 binary
• Steps
• Normalize to have leading 1
s 4-bits exp 3-bits frac
• Round to fit within fraction
• Post-normalize to deal with effects of rounding
• Case study
Value Binary
128 10000000
13 00001101
17 00010001
19 00010011
138 10001010
63 00111111
Normalize
1.BBGRXXX
Guard bit: LSB of result Round bit: 1st bit removed Sticky bit: OR of remaining bits
• Round up conditions
• Round = 1, sticky = 1 > 0.5
• Round = 1, sticky = 0 round to even, to make G an even number
•Exponent E: E2
(–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, computer will represent the result by ∞ or - ∞
•Round M to fit frac precision
Floating point multiplication
• (–1)s1 M1 2E1 x (–1)s2 M2 2E2
• Fixing
• Round M to fit frac precision
• If M ≥ 2, shift M right, increment E
•Overflow if E out of range, computer will represent the result by ∞ or - ∞
• Implementation
• The biggest task is multiplying significands, taking a lot of time
Mathematical properties of FP Multiplication
• For the following questions, YES or NO?
• Multiplication Commutative (a*b = b*a)?
• Multiplication Associative ((a*b)*c = a*(b*c))?
• Possibility of overflow, inexactness of rounding
• Eg: (1020 * 1020)*10-20= inf,
1020 * (1020 * 10-20)= 1020
• 1 * a = a?
• Multiplication distributes over addition (a*(b+c) = a*b + a*c)?
• Possibility of overflow, inexactness of rounding
1020 *(1020 - 1020)= 0.0,
1020 * 1020 – 1020 * 1020 = inf – inf NaN
Mathematical properties of FP addition
• For the following questions, YES or NO?
• Commutative (a+b = b+a)?
• Associative ((a+b)+c = a+(b+c))??
• Overflow and rounding
• (3.14 + 1020) – 1020 = 0.0
• 3.14 + (1020 – 1020) = 3.14
• 0 is additive identity?
• Every element (x) has an additive inverse (exists x’, so that
x+x’=0)?
− +
−Normalized −Denorm +Denorm +Normalized
NaN NaN
0 +0
Thank You