Thread: Round off error
View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
harvey harvey is offline
external usenet poster
 
Posts: 24
Default Round off error

Okay try to run this code in VBA and see the reults in immediate window.
sub test
Dim i as double
for i= - 0.2 to 0.2 step 0.1
debug.print i
next
End sub
you must get "0" in the third step but you get a very small number well
this is a very common and known round up error but I want to know is there
any quick fix or trick to eliminate this error in VBA code?
Thanks:)
"Vergel Adriano" wrote:

Sorry, but I still don't understand what the problem is. What "error" are
you trying to eliminate? Maybe if you give a little more detail on what you
intend to do with the loop or what it is that you're trying to get done.

"Harvey" wrote:

Sorry! my mistake I forgot to write that step now if you run it you won't get
0 as you expect!! instead very very small number how can we modify the code
to eliminate this error.
Thanks :)

"Vergel Adriano" wrote:

Harvey,

I'm not sure of what you mean by "eliminate roundoff" and what you expect
the loop to do, but the way you have it, it will go through the loop only
once and that's for the first value of -0.2. The next value will be 0.8
which is 0.2. If you specify a step value, you'll get more iterations:

Dim i As Double
For i = -0.2 To 0.2 Step 0.1
Debug.Print i
Next


"Harvey" wrote:

Anyone knows how to eliminate roundoff error in a loop?

Dim i as double
for i= - 0.2 to 0.2
debug.print i
next

Thanks:)