View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
JLatham JLatham is offline
external usenet poster
 
Posts: 3,365
Default A Serious Round-Off Problem !!

First, I'd change the name of SUM to sumthing else. Not saying that using
the name of a Worksheet function is affecting results, but why take a chance?
Maybe use myTotal or myInterimResult

See what your current SUM value is before the multiplication in Test2() -
put a breakpoint at the RESULT2=SUM*1000 statement or a STOP command just
ahead of it. Use the immediate window to Print SUM and see whether or not
that value is the same as calculated in TEST1 (capturing value of SUM at same
point in that code via same method). Since you indicate more code follows
this, want to make sure that nothing is affecting the contents of RESULT1 or
RESULT2 later on.

"monir" wrote:

Hello;
The following two sample codes produce two completely different results !!
SUB TEST1()
' ... my code...
SUM = 0.0
FOR I = 1 TO 20000
SUM = SUM + A(I)*B(I)/C(I)
NEXT
RESULT1 = SUM
' ...my code...
END SUB

SUB TEST2()
' ... my code...
SUM = 0.0
FOR I = 1 TO 20000
SUM = SUM + A(I)*B(I)/C(I) /1000.
NEXT
RESULT2 = SUM*1000.
' ...my code
END SUB

Variables: Result1, Result2, SUM, A,B,C are DOUBLE data types
Values of A,B,C vary from very small to very large, in no specific order
For a typical set of analytical data, Result1 = 0.9986, while Result2 =
4.2432, a considerable difference !!

The different results are most likely associated with accumilated round-off
errors known to be associated with floating-point arithmetic precission.

I would very much appreciate your suggestion(s) on how to minimize this
serious round-off error.
Can one use a higher precission than DOUBLE ... either in VBA or FORTRAN ??

Thank you kindly.