Don't trust Int
No attachments for this post
The Int function can return incorrect results. Don't use it directly, but instead wrap it in your own function to make the problems disappear.
Original Author: Mel Grubb II
Already since submitting the article (About an hour ago), there has been a lot of feedback and we've collectively found the following things:
1) The problem only appears in the IDE.
2) The FIRST time you use Int in the IDE, it works correctly. Subsequent calls return incorrect results.
Thanks to Sean Street for his feedback.
I'm going to clear out some of the comments to lessen the confusion now that we seem to have a better grip on the problem.
Original Article Follows:
While tracing through some game code the other day, I noticed a wrong number. I very carefully evaluated every part of the statement, and discovered that the bug lay within the Int function itself. You can reproduce this in the immediate window in one line.
Go to the Immediate (Debug) window and type the following:
? Int(0.7 * 10)
It will say 6. EXCUSE ME? The integer of 7 is six? If you put 7 in the parenthesis, you will get the answer 7. It is only when you pass a calculation into the function that the results come back wrong.
The truly amazing part is that Microsoft has alrady found and fixed this bug once before. Way back in version 4. Check out this KB article:
Well, I guess we wait for Microsoft to fix it AGAIN, but in the meantime we can write a function to "wrap" the int function so that you are not passing it a calculation any more. I've called mine mInt for "Make Integer" because CInt is already taken (And by the way, behaves differently as I'll describe below).
Public Function mInt(ByVal Value As Double) As Integer
mInt = Int(Value)
By passing the calculation into this function, we are forcing it to evaluate down into a single variable (Value). This seems to eliminate the problem.
As I said above you can't just use CInt instead of Int because they act differently. In immediate mode type the following:
You get 4, right? Now type:
You get 5. CInt rounds numbers when converting them, so it's useless for replacing Int which simply truncates the fractional portion of a number.
This sort of bug is simply unacceptable. To get incorrect results from one of the basic, fundamental building blocks of a language calls the reliability of the whole language into question.
Get this, Microsoft doesn't even PRETEND that they are going to acknowledge your bug report any more. Most companies respond to bug reports, but M$ doesn't even have a spot on the form to put your email address any more. This guarantees that you'll never get so much as a "Thank you" from the evil empire. I can also pretty much guarantee that you won't see this bug acknowledged on the website until it has been fixed.
Comments on this post
No comments have been added for this post.
You must be logged in to make a comment.