Download presentation
1
Dr Damian Conway Room 132 Building 26
Real Number Representation (Lecture 25 of the Introduction to Computer Programming series) Dr Damian Conway Room 132 Building 26
2
Some Terminology All digits in a number following any leading zeros are significant digits:
3
Some Terminology The scientific notation for real numbers is: mantissa base exponent
4
Some Terminology The mantissa is always normalized between 1 and the base (i.e. exactly one significant figure before the point): Normalized Unnormalized B.139FC B1.39FC 2-1
5
Some Terminology The precision of a number is how many digits (or bits) we use to represent it. For example:
6
Representing numbers A real number n is represented by a floating-point approximation n* The computer uses 32 bits (or more) to store each approximation. It needs to store the mantissa, the sign of the mantissa, and the exponent (with its sign).
7
Representing numbers So it has to allocate some of its 32 bits to each task. The standard way to do this (specified by IEEE standard 754) is:
8
Representing numbers 23 bits for the mantissa;
1 bit for the mantissa's sign (i.e. the mantissa is signed magnitude); The remaining 8 bits for the exponent.
9
Representing numbers 23 bits for the mantissa;
1 bit for the mantissa's sign (i.e. the mantissa is signed magnitude); The remaining 8 bits for the exponent.
10
Representing numbers 23 bits for the mantissa;
1 bit for the mantissa's sign (i.e. the mantissa is signed magnitude); The remaining 8 bits for the exponent.
11
Representing numbers 23 bits for the mantissa;
1 bit for the mantissa's sign (i.e. the mantissa is signed magnitude); The remaining 8 bits for the exponent.
12
Representing the mantissa
Since the mantissa has to be in the range 1 ≤ mantissa < base, if we use base 2 the digit before the decimal has to be a 1. So we don't have to worry about storing it! That way we get 24 bits of precision using only 23 bits.
13
Representing the mantissa
Those 24 bits of precision are equivalent to a little over 7 decimal digits:
14
Representing the mantissa
Suppose we want to represent : That means that we can only represent it as: (if we truncate) (if we round)
15
Representing the mantissa
Even if the computer appears to give you more than seven decimal places, only the first seven are meaningful. For example: #include <math.h> main() { float pi = 2 * asin(1); printf("%.35f\n", pi); }
16
Representing the mantissa
On my machine this prints out:
17
Representing the mantissa
On my machine this prints out:
18
Representing the exponent
The exponent is represented as an excess-127 number. That is: – – +128
19
Representing the exponent
However, the IEEE standard restricts exponents to the range: –126 ≤ exponent ≤ +127 The exponents –127 and +128 have special meanings (basically, zero and infinity respectively)
20
Floating point overflow
Just like the integer representations in the previous lecture, floating point representations can overflow: 10128
21
Floating point overflow
Just like the integer representations in the previous lecture, floating point representations can overflow: 10128
22
Floating point overflow
Just like the integer representations in the previous lecture, floating point representations can overflow: ∞
23
Floating point underflow
But floating point numbers can also get too small: ÷
24
Floating point underflow
But floating point numbers can also get too small: ÷
25
Floating point underflow
But floating point numbers can also get too small: ÷
26
Floating point addition
Five steps to add two floating point numbers: Express them with the same exponent (denormalize) Add the mantissas Adjust the mantissa to one digit/bit before the point (renormalize) Round or truncate to required precision. Check for overflow/underflow
27
Floating point addition example
y = 106
28
Floating point addition example
1. Same exponents: x = 107 y = 107
29
Floating point addition example
2. Add mantissas: x = 107 y = 107 x+y = 107
30
Floating point addition example
3. Renormalize sum: x = 107 y = 107 x+y = 108
31
Floating point addition example
4. Trucate or round: x = 107 y = 107 x+y = 108
32
Floating point addition example
5. Check overflow and underflow: x = 107 y = 107 x+y = 108
33
Floating point addition example 2
y = 10-5
34
Floating point addition example 2
1. Same exponents: x = 10-5 y = 10-5
35
Floating point addition example 2
2. Add mantissas: x = 10-5 y = 10-5 x+y = 10-5
36
Floating point addition example 2
3. Renormalize sum: x = 10-5 y = 10-5 x+y = 10-8
37
Floating point addition example 2
4. Trucate or round: x = 10-5 y = 10-5 x+y = 10-8 (no change)
38
Floating point addition example 2
5. Check overflow and underflow: x = 10-5 y = 10-5 x+y = 10-8
39
Floating point addition example 2
Question: should we believe these zeroes? x = 10-5 y = 10-5 x+y = 10-8
40
Floating point multiplication
Five steps to multiply two floating point numbers: Multiply mantissas Add exponents Renormalize mantissa Round or truncate to required precision. Check for overflow/underflow
41
Floating point multiplication example
y = 10-3
42
Floating point multiplication example
1&2. Multiply mantissas/add exponents: x = 105 y = 10-3 x y = 102
43
Floating point multiplication example
3. Renormalize product: x = 105 y = 10-3 x y = 103
44
Floating point multiplication example
4. Trucate or round: x = 105 y = 10-3 x y = 103
45
Floating point multiplication example
4. Trucate or round: x = 105 y = 10-3 x y = 103
46
Floating point multiplication example
5. Check overflow and underflow: x = 105 y = 10-3 x y = 103
47
Limitations Float-point representations only approximate real numbers.
The normal laws of arithmetic don't always hold (even less often than for integer representations). For example, associativity is not guaranteed:
48
Limitations x = 103 y = 103 z = 100
49
Limitations x = 3.002 103 x+y = 2.000 100 y = -3.000 103
z = 100
50
Limitations x = 3.002 103 x+y = 2.000 100 y = -3.000 103
(x+y)+z = 100 z = 100
51
Limitations x = 103 y = 103 z = 100
52
Limitations x = 3.002 103 y = -3.000 103 y+z = -2.993 103
53
Limitations x = 3.002 103 x+(y+z) = 0.009 103 y = -3.000 103
54
Limitations x = 3.002 103 x+(y+z) = 9.000 100 y = -3.000 103
55
Limitations x = 3.002 103 x+(y+z) = 9.000 100 y = -3.000 103
56
Limitations Consider the other laws of arithmetic:
Commutativity (additive and multiplicative) Associativity Distributivity Identity (additive and multiplicative) Spend some time working out which ones (if any!) always hold for floating- point numbers.
57
Reading (for the very keen)
Goldberg, D., What Every Computer Scientist Should Know About Floating- Point Arithmetic, ACM Computing Surveys, Vol.23, No.1, March 1991.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.