Why 0.1+0.2=0.30000000000000004(floating point number addition)

From the how-to-store-numbers-in-javascript, we know that

0.1 
= 
0 (sign)
01111111011 (exponent)
1001100110011001100110011001100110011001100110011010(mantissa)
=
0.10000000000000000555111512312601

Now we get 0.2 by the same way,

0.2 
= 
0 (sign)
01111111100 (exponent)
1001100110011001100110011001100110011001100110011010(mantissa)
=
0.200000000000000011102230246252

Now we operate by the following steps:

1. these two exponents must be equal to each other, different exponents can't be operate,for example 10*23 + 10*22 (decimal operation),we need to change like this 
3*103 + 5*102 = 3*103 + 0.5*103 = 3500.

2. the exponent in 0.1 is smaller than that in 0.2, so 0.1 change to 0.1 
= 
0 (sign)
01111111100 (exponent)
(1)1001100110011001100110011001100110011001100110011010(mantissa) ,it's important to note that the first italic 1 is an invisible digit.

3. add two mantissa 0.1100110011001100110011001100110011001100110011001101 + 1.1001100110011001100110011001100110011001100110011010
= 10.01100110011001100110011001100110011001100110011001110

4. we kown that mantissa is greater than or equal to 1 and less than 2, so 10.01100110011001100110011001100110011001100110011001110 has been change to 1.0011001100110011001100110011001100110011001100110011(1) ,the last italic 1 need to be round it up =>1.0011001100110011001100110011001100110011001100110100

5. and we get 0 (sign)
01111111101 (exponent)
0011001100110011001100110011001100110011001100110100(mantissa) => (-1)0* (2)-2 *1.0011001100110011001100110011001100110011001100110100 => 0.010011001100110011001100110011001100110011001100110100 => 0.300000000000000044408920985006

6. 0.1+0.2 = 0.300000000000000044408920985006

Add a Comment

Your email address will not be published. Required fields are marked *