What is floating point?
Floating point is one of the ways computers represent real numbers. There's also fixed point, but you don't see it much in modern systems. It mostly shows up in places like embedded, where you want to squeeze more out of your memory.
% Base 10 example (real computers use base 2)
45.67 -> 4.567 x 10^2
0.00987 -> 9.87 x 10^(-3)Writing a number as significant digits (mantissa) * a power of 10 (exponent) like above is basically what floating point is. In everyday life we use base 10, so the example looks like that, but computers use powers of 2 instead.
Floating point in computers (IEEE 754)
How computers do floating point is set by a standard called IEEE 754. A number is split into three parts:
- Sign: whether it's negative or positive
- Exponent: the power of 2
- Mantissa: the significant digits
Let's take one apart and see how it looks from the computer's side.
32 bit vs 64 bit, different precision
| Part | 32 bit | 64 bit |
|---|---|---|
| Sign | 1 bit | 1 bit |
| Exponent | 8 bits | 11 bits |
| Mantissa | 23 bits | 52 bits |
With 64 bit systems, numbers got more bits to work with. That means you can handle floating point numbers that are more precise and cover a much wider range than on 32 bit.
Ignoring the sign, a 32 bit number covers roughly 10^(-38) to 10^38, while a 64 bit number goes from 10^(-308) all the way to 10^308. 10^(-308) is 0.0000000...001 with 308 zeros after the decimal point. That's really precise.
But all that changes is how many bits you get. The way the math works is exactly the same for 32 bit and 64 bit.
Taking it apart
Say we're on a 32 bit system and we have the binary number 0100001010100000000000000000000. This is easier than it looks. Split it into 1 sign bit / 8 exponent bits / 23 mantissa bits, work out each part, then multiply them all together. Just don't forget about the bias in the exponent.
| Sign | Exponent | Mantissa | |
|---|---|---|---|
| Binary | 0 | 10000010 | 10100000000000000000000 |
| How to get | 0 is positive | Convert to decimal 130, subtract bias 127 |
Put the mantissa bits after the point in 1.x to get binary 1.101 |
| Decimal | +1 | 3 | 1.625 |
Going by the table, 0100001010100000000000000000000 is a positive number with an exponent of 2^3 and a mantissa of 1.625. Multiply it all out, (+1) * (2^3) * (1.625), and you get 13 in decimal. Done.
That's how you read a floating point number. The nice thing about floating point is that it uses memory efficiently, but the tradeoff is that it loses some precision. Let's look at that next.
Floating point isn't exact
With floating point, some real numbers just can't be stored exactly in binary. For example, 0.1 in binary turns into a number that never ends, 0.00011001100110011.... Let's see why.
Let's try to make 0.1
| Binary | Decimal |
|---|---|
| 0.1 | 0.5 |
| 0.01 | 0.25 |
| 0.001 | 0.125 |
| 0.0001 | 0.0625 |
| 0.00001 | 0.03125 |
| 0.000001 | 0.015625 |
| ... | ... |
We're going to build decimal 0.1 the dumb way, by adding numbers one at a time. Unlike whole numbers, that means adding smaller and smaller pieces that keep getting cut in half. Spoiler: no matter how you halve 1, you never land exactly on decimal 0.1. So the best we can do is add up small pieces to get as close to 0.1 as we can.
0.5 and 0.25 are way too big, so they're out. 0.125 is off by about 0.025, which is kind of close. But if we add 0.0625 and 0.03125 we get 0.09375, and adding even smaller numbers should get us closer.
So far we have binary 0.00011, which is decimal 0.09375, and we still need to make up a gap of 0.00625. To do that we need to find the right smaller numbers to add.
| index | Binary | Decimal |
|---|---|---|
| ... | ... | |
| 1 | 0.000001 | 0.015625 |
| 2 | 0.0000001 | 0.0078125 |
| 3 | 0.00000001 | 0.00390625 |
| 4 | 0.000000001 | 0.001953125 |
| 5 | 0.0000000001 | 0.0009765625 |
| ... | ... |
Now think of it as trying to make 0.00625 instead of 0.1. If we can make up that gap, we've got 0.1.
The numbers are getting tiny, so I'll use the index to keep things clear.
Number 2, 0.0078125, is pretty close to 0.00625. It's only off by 0.0015625.
But if we add number 3 0.00390625 and number 4 0.001953125, we get 0.005859375, which is even closer. Keep adding smaller numbers and the gap should close up.
Numbers 3 and 4 added together are 0.000000011 in binary. Add that to the 0.00011 from before and we're at 0.000110011.
Now we add smaller numbers again to fill the gap. But you probably already see where this is going. No matter how many times you repeat this, the gap never fully closes.
0.0001100110011
0.00011001100110011
0.000110011001100110011...
It just keeps repeating the same pattern forever. It's a number that never ends.
Floating point approximations
As we just saw, numbers that don't divide cleanly get stored as an approximation of a never ending binary number. How accurate that is depends on how many bits your system uses. Obviously 64 bit, with more digits, is more accurate. (You can still use the 32 bit float on a 64 bit system, of course.)
| Decimal | Actual value in 32 bit | Actual value in 64 bit |
|---|---|---|
| 0.1 | 0.100000001490116119384765625 | 0.1000000000000000055511151231257827021181583404541015625 |
| 0.2 | 0.20000000298023223876953125 | 0.200000000000000011102230246251565404236316680908203125 |
Because of these approximations, the code below doesn't do what you'd expect.
# Python
a = 0.1 + 0.2
print(a == 0.3) # False// JavaScript
const a = 0.1 + 0.2;
console.log(a === 0.3); // falseThis happens because floating point works with approximations. It's not a Python or JavaScript thing. You'll see the same thing in pretty much every language that uses IEEE 754.
Wrapping up
Floating point is a standard that lets you represent a huge range of real numbers while using memory efficiently. But because of how it works, precision becomes a problem when you deal with small numbers. So whenever you work with real numbers, keep in mind that there might be some error. If a finance app got this wrong, that would be a really big deal. (There's something called epsilon comparison that helps with this, worth a look.) Now that you know how floating point behaves, you can handle real numbers a bit more carefully.