Floating Point: The Decimal Coup
Today is March 14.
The civilians call this Pi Day and post circles. We will do something more useful.
We will discuss why computers lie about decimals in a disciplined, standardized, internationally recognized way.
This is not a bug. This is floating-point arithmetic.
I. The Basic Betrayal
Most programmers first meet floating point like this:
console.log(0.1 + 0.2);
// 0.30000000000000004
Then they go through the five stages of numerical grief.
The issue is not that the machine is stupid.
The issue is that decimal fractions such as 0.1 do not, in general, have exact finite representations in binary floating-point formats.
You asked for arithmetic. You received approximation with a standard attached.
II. The Standardized Form of the Lie
In 1985, IEEE 754 formalized binary floating-point arithmetic. This was one of the most important truces in computing because it made floating-point behavior more portable and more predictable across hardware and software.
That does not mean it made floating point intuitive.
It made it consistent enough to litigate.
At a high level, a binary floating-point value consists of:
- sign
- exponent
- significand (mantissa, informally)
For the common double / binary64 layout:
| Field | Bits | Role |
|---|---|---|
| Sign | 1 | Positive or negative |
| Exponent | 11 | Scale factor with bias |
| Fraction | 52 | Precision payload for significand |
This gives you enormous dynamic range. It does not give you exact decimal accounting.
III. Why 0.1 Is Not 0.1
In decimal, 0.1 is simple.
In binary, it is a repeating fraction.
The machine stores the nearest representable value in the chosen format. Arithmetic then proceeds on these approximations, with rounding rules applied at each step.
That means:
- input is approximated
- operations are rounded
- output may expose the residue
This is not moral failure. It is finite precision.
IV. What the Standard Actually Bought Us
IEEE 754 did not only define formats. It standardized behaviors that matter in real programs:
- rounding modes
- infinities
- NaNs
- signed zero
- exceptional conditions
| Feature | Why it matters |
|---|---|
| Rounding to nearest, ties to even | Default behavior that reduces aggregate bias |
+inf / -inf | Defined overflow semantics |
| NaN | Propagates invalid computations without pretending they are valid numbers |
| Signed zero | Matters in functions, limits, and branch-sensitive numerical behavior |
People mock signed zero until a numerical method or branch table depends on it.
Then they become quiet.
V. The Practical Danger
Floating point is fine for:
- graphics
- physics approximations
- scientific work designed around error bounds
- large parts of systems and application programming
Floating point is dangerous when people use it casually for:
- money
- equality comparisons
- loop termination conditions
- serialization expectations that assume decimal exactness
This is why “just use a float” is not engineering guidance. It is a confession.
VI. A Better Mental Model
Use tolerance-aware comparisons when the domain permits it:
function nearlyEqual(a, b, epsilon = 1e-12) {
return Math.abs(a - b) <= epsilon;
}
console.log(nearlyEqual(0.1 + 0.2, 0.3));
// true
But even this is not universal doctrine.
Choosing epsilon badly is just another way to be wrong more politely.
For financial values, fixed-point or decimal representations are usually the adult answer.
VII. The Historical Violence
Floating point has accumulated famous embarrassments because people keep assuming numbers are simpler than they are.
| Incident | What failed | Why engineers remember it |
|---|---|---|
| Pentium FDIV bug | Incorrect division lookup entries in hardware | Floating-point mistakes can become reputational and financial disasters |
0.1 + 0.2 class of surprises | Representation misunderstanding | The beginner’s first lesson in machine arithmetic realism |
| Financial rounding bugs | Domain mismatch | Approximation in the wrong problem space becomes liability |
The Supreme Leader considers the Pentium FDIV bug an instructive reminder that even the empire of arithmetic can ship errata.
VIII. The Real Story (Suppressed)
Officially, floating point is a numerical representation format.
Unofficially, it is a pact between mathematics and hardware in which both sides agree to stop demanding purity so the machine can finish before the heat death of the universe.
Pi Day encourages citizens to celebrate one irrational number. Floating-point engineering requires living with thousands of tiny rational compromises.
This is why the serious people are less impressed by 3.14159 than by correctly bounded error propagation.
The Decree
Today is March 14, and the appropriate response is not to worship pi in decorative typography.
The appropriate response is to remember:
- computers represent many decimals approximately
- standards matter because approximation without rules becomes chaos
- equality on floats is a loaded weapon
- domain choice matters more than clever patches
Floating point is not broken. It is a machine compromise with clearly documented terms.
Programmers get hurt when they do not read the treaty.
— Kim Jong Rails, Supreme Leader of the Republic of Derails