Amol Bavannavar Amol Bavannavar - 6 days ago 6
Vb.net Question

Incorrect Mod result in VB.NET

I'm getting incorrect Mod operator result in VB.NET. I verified result with calculator, It gives correct result.

E.g In VB.NET

1.3 Mod 0.05 = 0.049999999999999975
whereas in Calculator it shows
0

Answer

It's round up error:

1.30000000000001 Mod 0.05 == 0.00000000000001 
1.2999999        Mod 0.05 == 0.0499998999999999

usually, round up errors are little (if any) nuisance, put at / near points of discontinuity small errors lead to big difference at the result (0.05 in this case).

Amendment: change the initial double to either int (long)

130 Mod 5 == 0 

or decimal:

1.3M Mod 0.05M == 0