I have developed the a small program to calculate the addition of 2 numbers which are double. There seems to be some problem with the output specifically for the following 2 numbers 10.4 and 19.05. The output which I get after adding these two numbers is 29.450000000000003.
In case I add 10.4 and 29.05, I get the output as 39.45.
Can someone help me understand the reason for this discrepancy?
The reason is that floating point numbers can not represent every number exactly. They are generally an approximation of the actual number.
The two doubles both deviate a little bit from the real intended value, and when you add them, their deviation is added as well. In this particular case, the deviation becomes to big that it appears significant, and shows up in the output.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.