Home |
Search |
Today's Posts |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Here are some links to WebPages that will show you the problem in more
detail... INFO: Visual Basic and Arithmetic Precision http://support.microsoft.com/default...NoWebContent=1 (Complete) Tutorial to Understand IEEE Floating-Point Errors http://support.microsoft.com/default...NoWebContent=1 What Every Computer Scientist Should Know About Floating Point http://docs.sun.com/source/806-3568/ncg_goldberg.html Rick "Rick Rothstein (MVP - VB)" wrote in message ... The problem has to do with floating point representation and the inability of VB (actually, any language) of being able to store all fractions in a given integer data type (the problem is akin to the decimal representation of 2/3, a decimal point followed by an unending string of 6's, invariably the number is written as a fixed number of 6's followed by an ending 7)... whatever has accumulated in the loop variable "i" at the 1.9 stage, adding 0.1 to it gives a number ever so slightly greater than 2 which is outside of the ending loop limit, hence the loop ends at the 1.9 value. The way around this problem is to not use floating point numbers. For example... Sub test() For i = 15 To 20 Debug.Print i / 10 Next i End Sub (note the modified loop limits) where whenever you would have used "i" in your original loop code, you would use "i/10" instead. Rick "choi4u" wrote in message ... Running this code Sub test() For i = 1.5 To 2 Step 0.1 Debug.Print i Next i End Sub gives this output 1.5 1.6 1.7 1.8 1.9 . Why does the output not include 2.0? and what do I have to do to make the output include 2.0? Thanks in advance. |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
non-linear step count loop syntax? | Excel Programming | |||
Do loop until, step down row - copy paste - not what it seems. | Excel Programming | |||
How to increment within For Next Loop | Excel Discussion (Misc queries) | |||
Increment through For Next loop | Excel Programming |