I am working in JavaScript, but the problem is generic. Take this rounding error:
>> 0.1 * 0.2
0.020000000000000004
This StackOverflow answer provides a nice explanation. Essentially, certain decimal numbers cannot be represented as precisely in binary. This is intuitive, since 1/3 has a similar problem in base-10. Now a work around is this:
>> (0.1 * (1000*0.2)) / 1000
0.02
My question is how does this work?