How Computers Represent Numbers: Floating Point and Its Surprises

Fractional CTO · Founder of Injuria.ai
Open a console in almost any programming language and type 0.1 + 0.2. You will not get 0.3. You will get 0.30000000000000004. New programmers assume this is a bug in the language. It is not. The same value sits behind the scenes in JavaScript, Python, Java, C, and Rust, because they all store decimals the same way, and that way cannot represent 0.1 exactly. Understanding why is one of those pieces of knowledge that quietly separates people who get burned by number handling from people who do not.
Integers are the easy part
Computers store everything in binary, as sequences of bits that are either zero or one. Whole numbers map onto binary cleanly: the bits are place values that are powers of two, so 1011 is 8 plus 0 plus 2 plus 1, which is 11. Nothing is lost. Negative integers use a scheme called two's complement, which flips the bits of the positive value and adds one.[4] The clever result is that ordinary binary addition just works for negative numbers too, with no special case, which is why hardware adders are simple and fast.[4][6] The only real limit on integers is range: a 32-bit integer holds a bit over four billion values, a 64-bit integer far more. Stay inside the range and integer math is exact.
Fractions are where it gets hard
The trouble starts with fractions. We want to store numbers like 0.1, fit them in a fixed number of bits, and have the hardware work with them quickly. The format nearly every computer uses is IEEE 754, standardized in 1985, and it works like scientific notation in binary.[1][7] A 64-bit floating point number, the kind most languages call a double, splits its bits into three parts: one sign bit, eleven exponent bits that say where the binary point goes, and fifty-two significand bits that hold the digits.[8]
Two details make the format work and are usually glossed over. First, the significand has an implicit leading 1 that is not stored, so you actually get 53 bits of precision from 52 bits of storage, a free bit from a clever convention. Second, the exponent is not stored as a signed number but with a fixed bias of 1023 subtracted on read, which lets a simple unsigned comparison also order the numbers correctly. The payoff of the whole design is extraordinary range: it represents numbers from the subatomic to the astronomical in the same sixty-four bits, with roughly fifteen to seventeen significant decimal digits of precision.[2]
Why 0.1 cannot be stored
Here is the catch, and it has a precise mathematical cause. A fraction terminates in a given base only when the prime factors of its denominator divide that base. Base ten has factors 2 and 5, so tenths, fifths, and halves terminate, but thirds do not, which is why one third is 0.3333... forever. Base two has only the factor 2, so the only fractions that terminate in binary are those whose denominator is a power of two. One tenth is not, so 0.1 in binary is 0.0001100110011..., repeating forever.[3]
Since the significand has a finite number of bits, the computer cannot store the infinite pattern. It stores the closest value it can and rounds off the rest, so the number sitting in memory is not exactly 0.1, it is a hair above or below. Store 0.1 and 0.2, both slightly off, add them, and the two small errors combine into something visibly not 0.3.
0.1 + 0.2 // 0.30000000000000004
0.1 + 0.2 === 0.3 // false
(0.1 + 0.2).toFixed(2) // "0.30"
One honest footnote: languages differ only in what they show you by default, not in what they store. C's printf rounds to six digits unless you ask for more with something like %.17f, so it prints 0.300000 and looks innocent, while a JavaScript or Python console shows the full ugliness. The bits are identical. The deception is in the display.[5]
The rules that follow from this
Once you accept that floating point values are approximations, several practical rules fall out. Never compare them for exact equality; check whether the difference is smaller than a tiny tolerance, an epsilon. Be careful adding numbers of wildly different sizes, because adding a tiny number to a huge one can make the tiny one vanish entirely, which is why summing a long list into a running total accumulates error and numerical libraries use smarter summation. And watch for the special values: IEEE 754 defines positive and negative infinity, which you get from overflow or dividing by zero, and NaN, "not a number," from undefined operations like zero divided by zero. NaN has a famous property, it is not equal to anything including itself, so x !== x being true is the old trick for spotting one.
Money is the classic trap
The place this bites hardest is money. Representing dollars and cents as floating point invites one-cent discrepancies that accountants will find and you will not enjoy explaining. There are two standard fixes. The simplest is to store money as integers in the smallest unit, cents instead of dollars, so 19.99 becomes the integer 1999, all arithmetic is exact, and you divide by a hundred only to display. The alternative is a decimal type, provided by many languages and databases, that stores numbers in base ten and does exact decimal arithmetic at some cost in speed. Use integers or a decimal type for anything involving currency, and keep floating point for the measurements and scientific values it was designed for.
Knowing which ground you stand on
Floating point is not broken, and 0.1 + 0.2 is not a defect. It is the honest result of fitting an infinite binary fraction into sixty-four bits and rounding the remainder, the exact same compromise you make when you round one third to write it as a decimal. The real skill is not memorizing the quirks, it is knowing which representation you are standing on at any moment: integers are exact within their range, floating point trades exactness for enormous range and is right to about fifteen digits, and money belongs in neither of floating point's hands. Get that straight and you will know, every time, whether to trust the last few digits or to stop looking at them.
Sources (8)
- IEEE 754 (Wikipedia)
- David Goldberg, What Every Computer Scientist Should Know About Floating-Point Arithmetic
- The Floating Point Guide
- Two's complement (Wikipedia)
- MDN Web Docs: Number
- Ulrich W. Kulisch, Advanced Arithmetic for the Digital Computer (Springer Science & Business Media, 2012)
- David A. Patterson, John L. Hennessy, Computer Organization and Design (Morgan Kaufmann, 2008)
- William Stallings, Computer Organization and Architecture (Prentice Hall, 2006)