Here is a simple example:
Dim si As Single
For si = 2.6 To 3.3 Step 0.1
MsgBox si
Next si
The numbers I get are as follows (they are shown below as they are displayed in the MsgBox):
2.6
2.7
2.8
2.9
3
3.099999
3.199999
3.299999
So, I figured I'd change the data type from Single to Double to see what happens. This time, there are no .099999 type numbers, but the last item (iteration) is skipped, as in, missing, and doesn't exist. Here are the results as they appear in the MsgBox display):
2.6
2.7
2.8
2.9
3
3.1
3.2
I'm guessing in the case of the missing last iteration, it keeps track of it like the .299999 and never reaches .3 in its own mind, so skips it? However, the problem with this is that if this were happening, you'd get one of the previous iteration appearing twice for this to be able to happen, as it did in the Single Data Type example, where, if you truncated everything after the first decimal, you'd notice 3.0 appeared twice. First as "3" and then as "3.0" (if we ignore/truncate the 5 occurrences of the digit 9 after the "3.0" part - "3.099999").
Interestingly, another function I have where I loop through something using the Single Data Type displays the exact problem that our Double Data Type example above has, and none of the problems our Single Data Type example above has. So, the real reason why I am here is my Single Data Type loop missing and skipping the last item when using "Step 0.1" (being incremented by 0.1).
Is there a reliable way to do this using a "Step 0.1" and non-integer data types? If not, do we know the rules by which these chaotic things occur (in that, is the chaos predictable)? sigh
I thought you would all find this interesting, and I sure am looking forward to understand it. Searching Google was useless since as soon as I entered the work "for" and "step" into it, kept bringing up samples on how to use the step operator and nothing useful.
I would be interested in knowing how to live with this, or do this safely, and/or why it is happening in the first place. I would rather not loop using Integers for this particular function (and then divide up using another variable to get my decimal types), however if I have to and there is no reliable way to do it otherwise, I will. But I leave this to you guys, my programmer overlords.
My examples were occurring in VB6, however, I'm also wondering if this same chaos is occurring in VB.NET and even in C#? I'm assuming it's occurring in vba and vbscript. Any info on this would also be interesting and appreciated.
Thank you in advance.
To the Moderators (not necessary reading for the question): This is not a duplicate post, there may be other people posting floating-point issues in other languages (such as C#) but the solution to this (VB6) question is not necessarily the same as the solution for the C# question. There may be some solutions that are the same, but there will be solutions that are different as well, and there would be no way for me to get the different solution/answers from the C# post. As it turned out, the solution that I was interested in was a VB6 specific solution that would not have been found on the C# post that was referenced. I urge the mods to be diligent before attempting to close posts, I have seen post closed unfairly on many occasions and at the end of the day, you get to a stage where you are not adding any value, but removing substantial value from the Q and A style of this website. In conclusion, if there is even the possibility that there is new information/answers or solutions to be had by a similar looking post, you are better off refraining from closing. Especially if the questions are for a different language, even if the concepts are similar or the same, there are bound to be different solutions and solutions that are language specific.