Sid Rao1 month agoMost voted7
Binary can only store fractions exactly if the denominator is a power of 2.
0.25 = 1/4, 0.5 = 1/2, both exact, so the sum is exact.
0.1 = 1/10, and 10 has a factor of 5. In binary it becomes a repeating fraction (like 1/3 in decimal), so it gets rounded when stored. Same for 0.2. The two rounding errors add up and you get 0.30000000000000004.
For money just use integers (paise).