Numbers containing fractions are more difficult to represent. Real numbers consist of a mantissa and
an exponent. Computer designers decide how to allocate the bits of the computer word so that some can be
used for the mantissa and some for the exponent. In addition, the mantissa can be positive or negative, and the
exponent can be positive or negative.
You might imagine that different designers could create different definitions for real number formats.
A larger mantissa will provide greater precision; a larger exponent will provide for larger and smaller magnitudes
(scale). As recently as the 1980s, different computer manufacturers used different representations, and those
differences made it difficult to move data between computers, and difficult to move (“port”) programs from one
make of computer to another.
Since then, the IEEE has created a standard for binary floating-point number representation using 32 and
64 bits. The 32-bit format looks like this:
SEEEEEEEEmmmmmmmmmmmmmmmmmmmmmmm
The msb is the sign of the number, the 8-bit field is the exponent of 2, and the 23-bit field is the mantissa. The sign
of the exponent is incorporated into the exponent field, but the IEEE standard does not use simple two’s complement
for representing a negative exponent. For technical reasons, which we touch on below, it uses a different approach.
How would we represent 8.5? First we convert 8.5 to binary, and for the first time we will show a binary
fractional value:
1000.1
To the left of the binary point (analogous to the decimal point we’re familiar with) we have 8. To the right
of the binary point, we have 1/2. Just as the first place to the right of the decimal point in base 10 is a tenth, the
first place to the right of the binary point in base 2 is a half.
In a manner akin to using “scientific notation” in base 10, we normalize binary 1000.1 by moving the
binary point left until we have only the 1 at the left, and then adding a factor of 2 with an exponent:
1.0001 * 23
From this form we can recognize the exponent in base 2, which in this case is 3, and the mantissa, which is 0001.
The IEEE 32-bit specification uses a “bias” of 127 on the exponent (this is a way of doing without
a separate sign bit for the exponent, and making comparisons of exponents easier than would be the case with
two’s complements—trust us, or read about it on-line), which means that the exponent field will have the binary
value of 127 + 3, or 130. After all this, the binary representation of 8.5 is:
01000001000010000000000000000000
The sign bit is 0 (positive), the exponent field has the value 130 (10000010), and the mantissa field has the
value 0001 (and lots of following zeros).
As you can imagine, computing with real numbers requires the computer to do more work than
computing with integers. The mantissa and exponent fields must be considered appropriately in all mathematical
operations. In fact, some computers have special floating-point processor hardware to speed such
calculations.
No comments:
Post a Comment