Skip to content

Why does 0.1 + 0.2 not equal 0.3

0.1 + 0.2 does not equal 0.3 because 0.1 and 0.2 cannot be represented exactly in binary floating‑point, so the stored values are approximations that produce a tiny rounding error when added. In double‑precision arithmetic the result prints as 0.30000000000000004.

Computer Science · Number representation


Computers store real numbers using the IEEE 754 binary floating‑point format, which represents a value as a sign bit, a biased exponent, and a mantissa of binary digits. The mantissa holds a fraction in base‑2, so only fractions whose denominator is a power of two can be stored exactly. Decimal fractions such as 0.1 (one tenth) or 0.2 (one fifth) require an infinite repeating binary expansion, forcing the hardware to round them to the nearest representable binary fraction. The rounding follows the “round‑to‑nearest, ties‑to‑even” rule, which can introduce a tiny error even before any arithmetic is performed.

Binary fractions of decimal tenths

When 0.1 is converted to binary it becomes 0.0001100110011…₂, a pattern that never terminates. The double‑precision format keeps only 53 binary digits, so the stored value is actually 0.10000000000000000555… . Similarly 0.2 becomes 0.001100110011…₂ and is stored as 0.20000000000000001110… . Adding these two approximations yields 0.30000000000000004441…, which prints as 0.30000000000000004, not the exact 0.3. The extra bits that are discarded during conversion are the source of the mismatch you observe.

Equality tests compare the exact bit patterns of the stored numbers, not their mathematical ideals. Because the stored sum is slightly larger than the true 0.3, the expression (0.1 + 0.2 == 0.3) evaluates to false, while a test like (0.1 == 0.1) succeeds because both operands share the same rounded representation. This behavior explains why many programs appear to “lose” a cent in financial calculations when they rely on binary floating‑point for currency.

Common symptoms of binary rounding errors include:

  • 0.1 + 0.2 ≠ 0.3
  • 0.3 - 0.2 ≠ 0.1
  • Money calculations give unexpected cents

To see the exact stored values, follow these steps:

  1. 1Print the number with high precision, e.g., using format %.17g
  2. 2Convert the decimal to its binary mantissa using a tool or language function
  3. 3Subtract the printed value from the expected value to observe the error

Binary approximations of the numbers in double precision

DecimalBinary mantissa (first 16 bits)
0.10.0001100110011001
0.20.0011001100110011
Sum0.0100110011001100

To avoid surprising results, use one of three common strategies: work with integers (e.g., count cents instead of dollars), employ a decimal floating‑point or arbitrary‑precision library that stores base‑10 fractions exactly, or compare floating‑point numbers with a tolerance such as (|a-b| < 10^{-9}). Each approach eliminates the hidden binary rounding error in its own way, allowing reliable equality checks and accurate monetary totals.

Check yourself

What is the primary reason 0.1 + 0.2 does not equal 0.3 in binary floating‑point arithmetic?

Get this as a lesson built for you

Describe what you are studying and Lernex writes the lesson and the questions around it. Free, and it takes about a minute.

Try it

No account needed to try it.

What people ask next