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):
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:
The presentations of 0.1 and 0.2 in binary are not 100% exact themself, so the total is not exact as the 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 choose1.101 x 2^2
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
: - Step 2: Fill number with the formula to find Sign, Mantissa and Exponent:
- Step 3: Merge each part to create the final result:
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)
Image from
Wikipedia