Skip to the solution
MediumFrontend4 min read

Why is 0.1 + 0.2 not exactly 0.3?

666 views

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 answer β€” 0.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

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

4 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

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

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 72 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What are the spread and rest operators?

Continue