annoying little quirk

the first one prints out to 2.9, the second prints out 3

Sub test()
Dim x As Double
For x = 1# To 3# Step 0.1
Debug.Print x
Next x
End Sub

Sub test()
Dim x As Double
For x = 1# To 3# Step 1
Debug.Print x
Next x
End Sub

http://stackoverflow.com/questions/29558276/how-to-make-for-loop-work-with-non-integers

Floating point arithmetic will eventually screw you if you use a Double (or Single) as counter.”

Sub test()
Dim x As Double
Dim i As Integer

For i = 0 To 20
x = 1 + (i * 0.1)
Debug.Print x
Next i

End Sub
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s