Re: Round off error



Hmmm...

For me, I get zero in the third step, not a very small number. However
the principle is correct - rounding errors occur when you use floating
point math.

You could force the zero with

Public Sub Test2
Dim i As Long
Dim di as Double
For i = -2 to 2
di = i/10
debug.print i, i/10
Next i
End Sub

Ultimately, though, the only way to prevent all rounding errors is to
use only integer math. Most numbers can't be exactly represented in
binary, just as most numbers can't be exactly represented in decimal.


In article <55C6C587-ACF7-4BA2-920A-F1ED7C380FD6@xxxxxxxxxxxxx>,
Harvey <Harvey@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

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:)
.



Relevant Pages

  • Re: Value close to zero.
    ... floating point value is zero may become a problem. ... Just comparing some quantity x with matlab's 'eps' is not an adequate test ... are likely to differ from zero only because of rounding errors, ...
    (comp.soft-sys.matlab)
  • Re: Weirdness: (-0.666667 + 0.333333)
    ... > becuase of rounding errors. ... > I'm writing a program that draws graphs on a cartesian plane, ... The only thing you can do is to compare your value of x to zero ...
    (comp.lang.cpp)
  • Re: Weirdness: (-0.666667 + 0.333333)
    ... x - epsilon() < 0 ... >>becuase of rounding errors. ... The only thing you can do is to compare your value of x to zero ...
    (comp.lang.cpp)
  • Re: sorry .09 instead of .08
    ... zero wrote: ... you're in the same boat as using a normal double when it comes to rounding errors. ... rounding errors (plus overflow errors for added fun). ...
    (comp.lang.java.help)
  • Re: Modification in the CODE to HIDE rows and columns that start with ZERO (code given)
    ... Sub ABC() ... Dim r as Range, r1 as Range, r2 as Range ... Dim cell as Range ... and columns that begin with the number ZERO. ...
    (microsoft.public.excel.programming)