Skip to solution
mediumFrontend

Why is 0.1 + 0.2 not exactly 0.3?

666 views
01

Understand the problem

Question presented to candidate: "If I type 0.1 + 0.2 into a JavaScript console, what do I get, and why? And how would you correctly compare two floating-point numbers for equality in real code?"

What a strong answer should cover:

  • 📌 Interview term: IEEE-754 double-precision floating point — every JavaScript number is stored in this 64-bit binary format, which can only exactly represent certain fractional values (those expressible as a sum of powers of two). Most decimal fractions, including 0.1 and 0.2, have no exact binary representation and must be rounded to the nearest representable double.
  • 📌 Interview term: the real answer0.1 + 0.2 genuinely evaluates to 0.30000000000000004, not 0.3, because both operands are already-rounded approximations, and their sum's rounding compounds the error. Verified directly via .toPrecision(20), showing the actual stored bits differ from the mathematically exact 0.3.
  • A precise answer names that this is not unique to JavaScript — it affects every language using IEEE-754 doubles (Python, Java, C, Go), since it is a property of the number format itself, not a JavaScript-specific bug.
  • 📌 Interview term: Number.EPSILON — the smallest representable difference between 1 and the next larger double; the standard correct way to compare floats for "close enough" equality is Math.abs(a - b) < Number.EPSILON (or a domain-appropriate tolerance), never ===.
  • A precise answer names a real counter-example: 0.5 + 0.25 === 0.75 is genuinely true, because 0.5 and 0.25 are both exact powers of two — the bug only affects fractions that are not powers of two, not "all floating-point math."

Clarifying questions expected:

  • None — this is a definitional/technical question; precisely naming the IEEE-754 mechanism (not just "floating point is imprecise") is the strong signal.

Code / implementation expected: Optional — showing the toPrecision(20) output and the Number.EPSILON comparison fix demonstrates real understanding beyond reciting "floats are weird."

floating-point
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every number below, including the raw bit-level rounding, was actually printed by Node —

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal IEEE-754 rounding proof, Number.EPSILON fix, and the power-of-two counter-example
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 72 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track