[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: A still better method (MRO revisited)



PureBytes Links

Trading Reference Links

Warning, dweeby numerical analysis stuff follows.  :-)

Mark Jurik <mark@xxxxxxxxxxxx> wrote:
> In floating point, there is a limit as to how large it can get
> before successively adding a one will not have any effect. 

True, although of course that wouldn't cause the integer overflow you 
mentioned.  And it's very unlikely to cause a problem in Pierre's 
example.  A quick test shows that TS floats can increment to well 
over 16,000,000 before adding 1 has no effect.  Your system would 
have to run for a loooong time before you ran into that.  :-)  
However, it turns out Pierre's test would fail much sooner, but it 
would still work for over 1,000,000 bars.

Here's an indicator that shows the failure points.  I started out 
with a "while (count <> count+1)" test, but interestingly enough, the 
"<>" test fails long before incrementing fails!!  This code shows 
that "count = count+1" is true for even values starting at 1,048,576, 
or 2^20.  Incrementing fails at 16,777,216, or 2^24.  Apparently 
addition uses a 24-bit mantissa field, but the equality test uses 
only 20!  (Note that the equality test is only correct on every 16th 
value when we approach 2^24, due to the 4-bit difference in accuracy.)

I printed 9 digits after the decimal point to look for any loss of 
accuracy (e.g. 1048798.9999997).  As you'd expect, if we're within 
the mantissa limits of the float format, there was none.

        vars: count(0),count2(0);
        
        if (count = 0) then begin
          print("First find where equality test fails:");
          for count = 1048550 to 1048580 begin
            print("count: ",count:9:9,",   count+1: ",count+1:9:9,
                  ",  equal = ",count = count+1);
            end;
        
          print("Now find where incrementing fails:");
          count = 16777190;
          for count2 = 1 to 30 begin
            print("count: ",count:9:9,", count+1: ",count+1:9:9,
                  ",  equal = ",count=count+1);
            count = count+1;
            end;
          end;
        
        if (1 = 2) then plot1(0,"");

Gary