Floating Point: Why 0.1 + 0.2 Isn't 0.3
IEEE 754, Rounding Error, and the Bits Behind Every Decimal — A TLDR Primer
You typed 0.1 + 0.2 into a console and got 0.30000000000000004. Now what? This primer explains exactly why that happens and what to do about it — without sending you down a rabbit hole of computer architecture textbooks.
Written for high school and early-college students meeting this for the first time in a CS class, a coding bootcamp, or a job interview, the book walks through how computers actually store fractional numbers in binary, why decimals like 0.1 turn into repeating expansions the same way 1/3 does in base 10, and how the IEEE 754 standard packs a number into sign, exponent, and mantissa fields. From there it covers machine epsilon, why rounding error can quietly grow or suddenly explode through cancellation, and the practical coding rules that keep it from wrecking your program — never compare floats with ==, use tolerances, keep money in integers, and know when to reach for a Decimal or fractions library.
A closing chapter tours the real-world stakes: the Patriot missile clock drift, the Ariane 5 crash, jittery game physics, and the scientific-computing bugs that make this one of the most common interview questions in software engineering. No filler, no derivations you'll never use — just the concrete model you need to reason about floating point correctly.
Ideal as a computer science exam prep guide, a quick refresher before a coding interview, or a plain-language answer for a parent trying to understand why their kid's homework code gives weird decimals.
Stop guessing why your numbers don't add up — get the short version that actually explains it.
- Explain why 0.1 + 0.2 evaluates to 0.30000000000000004 in most programming languages
- Decode a number stored in IEEE 754 single- or double-precision format
- Distinguish rounding error, machine epsilon, and catastrophic cancellation
- Compare floating-point numbers safely using tolerance-based checks
- Recognize when to use integers, decimals, or arbitrary-precision libraries instead of floats
- 1. The 0.1 + 0.2 ProblemOpen with the famous surprise, show it in real code, and set up the question the rest of the book answers.
- 2. Binary Fractions: Why Some Decimals Don't FitExplain how fractions work in base 2, why 0.1 becomes a repeating binary expansion, and draw the analogy to 1/3 in base 10.
- 3. IEEE 754: How a Float Is Actually StoredWalk through the sign, exponent, and mantissa fields of single- and double-precision floats, with a fully decoded example.
- 4. Rounding, Machine Epsilon, and Error That GrowsIntroduce round-to-nearest-even, machine epsilon, and how errors accumulate or explode through cancellation.
- 5. Writing Code That Survives Floating PointPractical rules: never use == on floats, use tolerances, prefer integers for money, and know when to reach for Decimal or fractions libraries.
- 6. Where This Matters: From Games to RocketsShort tour of real-world consequences — Patriot missile failure, Ariane 5, graphics jitter, scientific computing — and why every programmer eventually meets this bug.