PDA

View Full Version : Solved: Need help with looping function and Rounding issue



nitt1995
08-18-2006, 06:51 AM
I have a loop that increments a fractional number. In the first case (0,2 step .4) it works as desired, with 2 included. In the second case (3,5 step .4), it skips 5.

What is the basis for this difference, or is there a bug? What is the work-around?


Sub testLoopInc()
Dim Min As Double, Max As Double, Inc As Double
Dim d As Double
Min = 0#
Max = 2#
Inc = 0.4
d = Min
Debug.Print "0 to 2 , step .4"
Do While d <= Max
Debug.Print d
d = d + Inc
Loop
Debug.Print "now 3 to 5 , step .4"
Min = 3#
Max = 5#
Inc = 0.4
d = Min
Do While d <= Max
Debug.Print d
d = d + Inc
Loop
d = Min
End Sub

nitt1995
08-18-2006, 07:25 AM
Nevermind. I guess this is just standard floating point issue. I made workaround that doesn't increment by floats..