Floating Point Error in For/Next Loop Counter

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,025
Well, I'm glad this just happed to occur in something where it wasn't mission critical...

Try this:
Code:
Sub foo()
    For p = -1 To 1 Step 0.2
        With Cells(1, (p + 1) * 5 + 1)
            .NumberFormat = "0%_);[Red](0%);0%"
            .Value = p
        End With
    Next p
End Sub
  • What do you get in F1? I'm getting -5.55111512312578E-15%
  • What do you get in K1? I'm getting nothing: the loop does not execute for p=1
This is obviously a floating point error, but I hadn't ever thought about it happening on a loop counter.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Number sizes can make a difference. Try...
Code:
Sub FP_Formats()
    Dim sngX As Single
    Dim dblY As Double
    Dim decZ As Variant
    
    sngX = 0
    dblY = 0
    decZ = CDec(0)
    
    For i = 1 To 10000
        sngX = sngX + 0.0001: dblY = dblY + 0.0001: decZ = decZ + 0.0001
    Next i
    
    MsgBox "Single: " & sngX & vbCrLf _
        & "Double: " & dblY & vbCrLf _
        & "Decimal: " & decZ
End Sub

Denis
 
Upvote 0
Interesting... Currency is the same as Decimal, correct:
Code:
Sub FP_Formats()
    Dim sngX As Single
    Dim dblY As Double
    Dim decZ As Variant, i%, crrA As Currency
 
    sngX = 0
    dblY = 0
    decZ = CDec(0)
    crrA = CCur(0)
 
    For i = 1 To 10000
        sngX = sngX + 0.0001: dblY = dblY + 0.0001: decZ = decZ + 0.0001: crrA = crrA + 0.0001
    Next i
 
    MsgBox "Single: " & sngX & vbCrLf _
        & "Double: " & dblY & vbCrLf _
        & "Decimal: " & decZ & vbCr _
        & "Currency: " & crrA
End Sub

Do you get the same results as I do when you run the code I posted?
 
Upvote 0
Yes on both counts. I got the same results as you, and Currency should also give the correct values because it's decimal instead of binary.

Denis
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top