Back to all blogs

Why 0.1 + 0.2 is not 0.3 in most languages?

April 6th, 2024 - 3 min read

A simple explanation of why we should not rely 100% on float calculation.


First, let's see how 0.1 and 0.2 can be represented in binary (which can be understood by computer):

0.1 0.2 explanation

As we can see, 0.1 and 0.2 are cyclic float numbers in binary format. Because each variable can allocate a limited number of bits (let's say 32 bits), we have to cut off the remaining cyclic to fit 32 bits. So 0.1 and 0.2 become this:

0.1 0.2 binary

The presentations of 0.1 and 0.2 in binary are not 100% exact themself, so the total is not exact as the result:

0.1 0.2 result

Deeper look at floating point numbers

In the real world, floating point numbers are normalized and displayed in a specific format. We will take a look at how the computer stores the floating point numbers.

  • First, we need to normalize the binary to have the format: 1.M x 2^N. Why do we need to normalize?

    Because the binary can be present in many ways. Example: 1.101 x 2^2 = 11.01 x 2^1 = 110 x 2^0 = .... We need to pick 1 standard so every computer will follow. Here we choose 1.101 x 2^2

    floating numbers formula

    Formula with 32 bits

  • Sign: 0 means a positive number, 1 means a negative number.

  • Mantissa: the number after the floating point after we normalize the original number.

  • Exponent: After subtracting the Exponent from the Bias, the result is the number of bits we need to move so the floating point will be in the correct place.

  • Bias: The offset to make the Exponent always positive. With 8 bits for the Exponent, the Bias = 2 ^ (8 - 1) - 1 = 127.

Explanation with 0.1:

  • Step 1: Normalize 0.1 into the expected format: 1.M x 2^N:
    0.1 binary format
  • Step 2: Fill number with the formula to find Sign, Mantissa and Exponent:
    each part of 0.1 in floating point format
  • Step 3: Merge each part to create the final result:
    final 0.1 in floating point format

Why should we need Bias instead of making Exponent = N (two's complement)?

  • Exponent is a signed number, by adding Bias, we make Exponent always positive (unsigned number) -> Easier when doing the comparison between 2 numbers (No need to check the number is unsigned or signed but still be able to compare)
    offset binary

    Image from

    Wikipedia