1
$\begingroup$

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?

$\endgroup$
4
  • 4
    $\begingroup$ 0.30000000000000004.com $\endgroup$ – JMoravitz Feb 12 at 16:03
  • $\begingroup$ @JMoravitz can you please check my process in the Google document. $\endgroup$ – bmchaitu Feb 12 at 16:05
  • $\begingroup$ @JMoravitz lol incredible. I love that that exists. $\endgroup$ – Cameron Williams Feb 12 at 16:17
  • $\begingroup$ You can show that 0.3 converted to floating point and 0.300...004... are different floating point values, to justify not rounding the displayed value to 0.3. Some languages specify the displayed decimal to be the shortest representation that converts to the floating point value. $\endgroup$ – Claude Feb 14 at 11:44
2
$\begingroup$

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'
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.