Transcript
Ask a computer to add zero point one and zero point two. You'd expect zero point three. Instead, you get this. Zero point three, fifteen zeros, and a four. It's not a bug. It's how almost every computer stores numbers that aren't whole. Today, we find out why.
So far, every number in this series has been a whole number: five, three, fifteen, minus seven. In decimal, the digits after the point are tenths, hundredths, thousandths: each place is ten times smaller. Binary works the same way, except each place is two times smaller: a half, a quarter, an eighth, a sixteenth. So in binary, point one is one half. Point one one is a half plus a quarter: zero point seven five.
Now let's write one tenth in binary. Which halves, quarters and eighths add up to exactly zero point one? A half is too big. A quarter, too big. An eighth, still too big. A sixteenth fits. That's zero point zero six two five, and we still need a little more. Keep going, adding a piece whenever it fits, and a pattern shows up: zero zero one one, zero zero one one, over and over. It never ends. It's like one third in decimal: zero point three three three, forever. A computer has to stop after a fixed number of bits. So the zero point one it stores isn't exactly one tenth. It's just very, very close.
So how many bits do we keep, and where does the point go? Scientists solved this long ago, for very big and very small numbers. A million is one times ten to the six. A millionth is one times ten to the minus six. A few digits, plus a power that says where the point goes. The point can move, or float. That's why this is called floating point. Computers do the same thing in binary. Zero point one becomes one point one zero zero one, one zero zero one, and so on, times two to the minus four.
Most programming languages store this in sixty-four bits, split into three parts. One bit for the sign: plus or minus. Eleven bits for the exponent: which power of two. Here, minus four. And fifty-two bits for the fraction: the digits after the point. The one before the point isn't stored at all. In binary, almost every number written this way starts with a one, so it comes for free. Now look at the very end. The last group should be one zero zero one, but the pattern gets cut off here, so it's rounded up to one zero one zero. So the stored zero point one is a hair above one tenth.
Rounding happens because floating point can only land on certain points. Between them are gaps. Here's a tiny version with only three fraction bits, so we can see them. Near zero, the points are packed tight. Further out, the gaps get wider. Real sixty-four-bit numbers have far more points. Near one, they're about two times ten to the minus sixteen apart. But the idea is the same: every result gets rounded to a point on the line.
Now we can add. Let's zoom in near zero point three, so far that we can see the points themselves. True zero point three falls between two of them. The closer one, just below, is what the computer calls zero point three. Our stored zero point one and zero point two were both a hair too big. So their exact sum lands here, right in the middle between the two points. For a tie, the rule is to pick the point whose last bit is zero. Here, that's the upper one. When it's printed, the computer shows just enough digits to tell that point from its neighbor: zero point three, fifteen zeros, and a four. So zero point one plus zero point two isn't equal to zero point three. They're less than a millionth of a millionth apart, but they differ.
That's why programmers almost never ask whether two floating-point numbers are exactly equal. They ask whether they're close enough. And money is usually counted in whole cents, as integers, so nothing gets rounded away. In return, floating point gives us an enormous range. The same sixty-four bits can hold the width of an atom and the distance to the farthest galaxies.
So: a computer stores a number as a sign, an exponent and a fraction, and most decimals can only be stored approximately. Next time: if everything inside is numbers, how does a computer store letters, colors and sounds? See you there.
Next episode: how text, colors and sound all become numbers.