What a Computer Does to a Number
Reading a Real Number Out of Its Bits, and Writing One Tenth Back In
Last timeWhat Happens at the Edge
The sign, exponent and fraction fields of a 32 bit and a 64 bit real, decoded by hand from one bit pattern, then a tenth normalised, expanded in binary, rounded, and quoted exactly.
Why the point is not simply nailed down
Thirty-two bits is patterns, and nothing about
the bits decides what they mean. A rule for reading them does. The simplest
rule for fractions is to nail the binary point to one place and leave it there:
read the top 16 bits as a whole number and the bottom 16 as a fraction. That
layout is honest and easy, and its limits are arithmetic. The largest magnitude
it names is , and the distance between neighbouring values
is , the same distance everywhere on the scale.
Now consider a calculation holding Avogadro's number, about ,
and the charge on an electron, about . The fixed layout
cannot name either one. Widening it does not help much, because a fixed point
layout spends bits on absolute resolution it does not need: to reach
in steps of takes over 140 bits, almost all of them recording
digits nobody measured.
So spend a few of the bits on where the point is instead. Take eight of the
thirty-two and read them as the power of two to scale by. Eight bits give
codes. Two of them are reserved, the all-zeros code and the
all-ones code, which is what the zeros, the infinities and the not-a-number
values are built out of. That leaves usable codes, and with a
bias of 127 they run from to .
The eight bits are not free. They come out of the fraction, which leaves 23
bits, and 23 bits of fraction is 23 bits of fraction at every magnitude. That
is the whole bargain, and it is worth saying plainly before any of the
decoding: the exponent field buys range, the fraction field buys precision, and
moving a bit from one to the other trades one for the other exactly.
Count what is left and finiteness stops being a warning and becomes a number.
Two signs, times 254 exponent codes, times fractions,
is ordinary values.
Subtract that from the patterns and
remain, which is , the two reserved
exponent codes with both signs: the zeros, the very small values, the
infinities and the not-a-numbers, all of them together.
| sign bits | exponent bits | fraction bits | bias | decimal digits carried | |
|---|---|---|---|---|---|
| 32 bit | 1.00 | 8.00 | 23.00 | 127.00 | 7.22 |
| 64 bit | 1.00 | 11.00 | 52.00 | 1023.00 | 15.95 |
The three fields
Three fields, read from the top of the word down: one sign bit, then the
exponent field, then the fraction field. Two of the three are not what they
look like.
The exponent field does not hold a signed exponent. It holds the exponent plus
a bias, so the stored field is never negative and no sign convention is needed
inside it. The bias is for a field of bits, so
for the 32 bit form and for the 64 bit form. The reason for
that particular offset is worth knowing, because it looks arbitrary and is not.
With the bias, the exponent codes increase as the magnitude increases, and the
exponent field sits immediately below the sign bit, so reading the entire 32 bit
word as an unsigned integer puts the positive reals in ascending order. A
machine can therefore compare two positive reals with integer hardware, and
sorting them is sorting their bit patterns.
The fraction field does not hold the significand either. Any non-zero value can
be written as some number in times a power of two, by shifting the
point until exactly one digit sits in front of it. In base two there is only one
non-zero digit available, so that leading digit is always a one. Storing it
would spend a bit on a constant. It is left out, and the 23 stored bits give 24
bits of significand. This is also why the all-zeros exponent code has to be
reserved: with no implied one there would be no way to write zero at all.
The lesson stops here
4 more paragraphs to go
You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.
The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.
See the planThe contentsThis is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents