CIS3355:
Business Data Structures |
Addendum 2.1: Floating-Point (Real) Numbers
We already know the basic layout for the data type float (from figure 2.20):
We also know that (generally), the left-most bit is the sign, and can take-on either ‘0’ or ‘1’ values.
Let's store the value 7.89, OR .789E+1 (i.e., 0.789 * 101). First, let's talk about how we would store the Exponent vale (+1).
The characteristic of the exponent is not that difficult to understand either. Since we have 7-bits, we have a total of 27 = 128 combinations, but since we know that the exponent can be either negative or non-negative, we really only have ˝ that number.
Not quite. The characteristic of the exponent is stored as a biased exponent. That means that rather than storing the sign and the value separately, we add (bias) a constant term to the true value. In our case, we would add the value 64 (which is ˝ of 128) to the true value. The exponent value –1710 (= 100012, or 00100012 on 7-bits) would actually be stored as the value –17 + 64 = 4710 (= 1011112, or 01011112 on 7-bits); the exponent value 2310 (=101112 or 00101112 on 7-bits) would actually be stored as the value 23 + 64 = 8710 (=10101112). In our case, the exponent value 1 would actually be stored as 1 + 64 = 6510 (= 10000012)
There are some technical reasons, which we need not go into, but of course, it does circumvent the step of having to store the sign and the value separately.
One final quick note on characteristics: the range of exponent values is actually –(26 – 1) through +(26 – 1), or –63 through +63. The binary representation 00000002 (the decimal value 0) is reserved for other uses.
Converting the mantissa to binary requires a somewhat different algorithm than we used to convert integers to binary, but it still has to do with exponent position. For example, the integer 456 would have the exponent positions shown at the left (in other words, 456 = 4*102 + 5*101 + 6*100). If we were to consider the real number 456.789, however, the exponent positions would appear as the do on the right (in other words, 456.789 = 4*102 + 5*101 + 6*100 + 7*10-1 + 8*10-2 + 9*10-3). Notice that the exponents for the mantissa are the inverse of the positions for the integer portion of the number.
The procedure we need to use is also the inverse of the procedure we used when converting a decimal integer to binary. Previously, we divided the integer portion by two and kept track of the remainders and collected reverse order received. Now, we need to multiply the mantissa by two and keep track of the quotients and collect in order received. In both cases, however, we can stop when the value to be multiplied or divided is 0.
The mantissa .789, for example could be converted to binary as follows:
And COLLECTING FROM THE TOP, the mantissa .789 would be stored (on 24-bits) as: 110010011111101111100111
and the value 7.89 would be stored as: 0 1000001 110010011111101111100111 (on 32-bits)
This is true. We previously noted that, unlike integers, not all mantissas can be converted to binary. Sometimes, they become an infinite series.
Much in the same way we checked integers. For example we know that the binary representation of the integer 45610 = 1110010002 can be checked as: 28 + 27 + 26 + 23 = 256 + 128 + 64 + 8 = 456
Since we know that the positions of the mantissa are the inverse of the integer positions, we know that binary mantissa 110100111111011111001110 (= .78910) can be associated with the positions:
Meaning that the expression could be checked as:
2-1 + 2-2 + 2-5 + 2-8 + 2-9 + 2-10 + 2-11 + 2-12 + 2-13 + 2-15 + 2-16 + 2-17 + 2-18 + 2-19 + 2-22 + 2-23 + 2-24
First, let’s approach it as a decimal mantissa. The decimal mantissa .789 could be rewritten as:
The binary mantissa (as with integers, ‘0’ bits are ignored) could be rewritten in the same fashion
» .789
We must first normalize the number:
456.789 = 45.6789 * 101 = 4.56789 * 102 = .456789 * 103
Where the last notation is what we will use to store the number:
Storing the first 8-bits (the sign and the characteristic) is relatively easy:
Sign: 0 (positive) Characteristic: 310 + 6410 = 6710 = 10000112 (on 7-bits) Sign and characteristic lay-out: 01000011 (using the first 8-bits)
Converting the mantissa .456789 as we did before
And COLLECTING FROM THE TOP, the mantissa (on 24-bits) is: 011101001111000000011111
Therefore, the real number +456.789 would be stored as:
01000011011101001111000000011111 (on 32-bits)
Sometimes, but not very often. Naturally, increasing the number of bits we allocate to the mantissa helps increase the precision of the mantissa, but it doesn’t always assure that we will be able to represent values exactly.
Yes.
That is beyond the realm of our discussion. However, there is enough information provided to allow the truly die-hard student to figure it out.
They are. As we noted previously, that is why some supercomputers indicate operating speed in terms of flops (floating-point operations per second).
|