Is this VBA integer conversion bug documented?

  • Thread starter Joe in Australia via OfficeKB.com
  • Start date
J

Joe in Australia via OfficeKB.com

I came across a bug recently and I was wondering if anyone else has
encountered it. When I calculate the expression in the routine below I get
one result if I print it and another result if I save it to a variable - even
if I just save the string representation of it! The subroutine runs the test
on three consecutive numbers, so you see it work correctly in two tests and
fail in a third.

Sub BuggyIntegers()
' This routine demonstrates inconsistent behavior in
' VBA's integer conversion

Dim MyVar As Integer
Dim Unity As Variant

Dim MyString As String: MyString = ""

' These numbers aren't magic - they just demonstrate the problem
' quickly. Change them to any integers >= 2 to explore other results
For MyVar = 16 To 18

' This variable should always equal 1.
Unity = Int(Log(MyVar) / Log(MyVar))

' But if it doesn't equal 1, it should at least equal itself!
MsgBox _
"Compare the values when MyVar equals " _
& MyVar _
& " : " _
& Unity _
& " " _
& Int(Log(MyVar) / Log(MyVar))

' Let's save the results so we can compare them later.
MyString = MyString & vbCr & Unity & " " & Int(Log(MyVar) / Log(MyVar)
)

Next MyVar

' Print the results in one go.
MyString = _
"And now look at the same results assigned to a variable!" _
& vbCr _
& MyString

MsgBox MyString

End Sub
 
T

Tony Jollans

I'll grant this is a little odd but it is just a symptom of the inherent
inaccuracy of floating point arithmetic.

The Int function returns the integer part of a number so if the floating
point result is 0.99999... (recurring), you get 0.

The CInt (convert to integer) rounds the result to the nearest integer so
0.99999... converts to 1.

Replacing Int with CInt in your code gives the right results.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top