Amol Bavannavar Amol Bavannavar - 1 year ago 112 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.


1.3 Mod 0.05 = 0.049999999999999975
whereas in Calculator it shows

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download