LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default for loop with step increment of 0.1

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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
non-linear step count loop syntax? Ker_01 Excel Programming 3 April 8th 08 04:11 PM
Do loop until, step down row - copy paste - not what it seems. justagrunt Excel Programming 0 December 8th 06 01:21 AM
How to increment within For Next Loop EagleOne Excel Discussion (Misc queries) 1 August 9th 06 04:18 PM
Increment through For Next loop hotherps[_7_] Excel Programming 3 April 8th 04 05:40 PM


All times are GMT +1. The time now is 09:35 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"