Bad Floating Point Addition <vb.net>

NeuralJack

Centurion
Joined
Jul 28, 2005
Messages
138
This is an odd problem, and I'm not sure how it's arising. But Given this code the floating point addition gets messed up by 0.000000000000001 every so often. Is there a way to stop it?

Code:
  Dim Single1 As Single = 0
  Dim AddAmount As Single = 0.001

  For i As Integer = 0 To 2000
      Single1 = Single1 + AddAmount
      Debug.WriteLine(Single1.ToString)
  Next


In the Debug Window This happens now and then:
Code:
0.025
0.026
0.027
0.028
0.029
0.03
0.031
0.032
0.033
0.034
0.035
0.03599999
0.03699999
0.03799999

or 

1.047
1.048
1.049
1.05
1.051
1.05199999999999
1.05299999999999
1.05399999999999
 
That's just the nature of the beast with floats. There are limitations to the accuracy when representing floats. Try using Decimal types instead.
 
Back
Top