0.1+0.2=0.30000000000000004 in floating point IEEE representations arithmetic addition manual proof.

arithmeticcomputational mathematicscomputer scienceeducationfloating point

I am trying to prove the 0.1+0.2=0.30000000000000004 by adding the numbers 0.1, and 0.2 in IEEE floating-point representation. I have added the operand values in IEEE format, with the general addition of bits the result I got is 1.0011001100110011001100110011001100110011001100110100 as mantissa, and the exponent value is 01111111101. Now When I tried to convert this binary number into a decimal number I have used the formula

$(-1)^s * $(1+mantissa) * $(2)^e$
, where s is sign bit and e stands for exponent bit value

where I need to calculate the 2^-48 and other values wherever the value is 1 in the result mantissa bits. I have used an online precision calculator to calculate the values of 2^-49, and others where the results are as below
negative exponent of 2 values

And the final decimal value I got is 0.3000000000000000444089209850062616169452667236328125 but the value I should get it as 0.30000000000000004, the answer which many of the programming languages give you.
Do not know where I am going wrong. Can anyone let me know If I am following any wrong process or my calculation part is going wrong?

Best Answer

Your value is the correct one, it's just that most programming languages don't print all those digits. But Python, for example, will show them if you ask for it:

>>> 0.1+0.2
0.30000000000000004
>>> '{0:.52f}'.format(0.1+0.2)
'0.3000000000000000444089209850062616169452667236328125'

And, for the record:

>>> '{0:.55f}'.format(0.1)
'0.1000000000000000055511151231257827021181583404541015625'
>>> '{0:.54f}'.format(0.2)
'0.200000000000000011102230246251565404236316680908203125'
Related Question