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

Re: Question Regarding Mathematical Operations in TS



PureBytes Links

Trading Reference Links

jedaz wrote:
 
> I would appreciate any help in understanding why the following happens
> (please see comments in the code):
> 
> var: foo1(0), foo2(0);
> 
> If LastBarOnChart Then Begin
> foo1 = 100.03125;
> print(foo1:0:5); { will print 100.03100 - should be 100.03125 }
> End;
> 
> If LastBarOnChart Then Begin
> foo2 = 100.03125*100;
> print(foo2:0:5); { will print 10003.10000 - should be 10003.125 }
> End;
> 
> I appears that TS truncates, or perhaps rounds values prior to performing
> mathematical operations - which is scary. I must be missing something simple
> here. I have looked in the TS2000i online manual and can find nothing
> regarding precision regarding TS mathematical operations.


I hate to get into this subject.  It has been hotly debated on this
list and others in the past.  Check the archives.

However, your particular test values are curious.  Note in Bob Fulks'
earlier reply to your message, and in many other places, that single
precision on a Wintel architecture using IEEE floating point
representation should be able to represent exactly numbers (ignoring
decimal point location) up to 16,777,216.  Anything larger is subject
to rounding, and the conversion from its binary representation back
to decimal form for output/display is also subject to errors.

Your number 10,003,125 is less than 16,777,216.  Hence, I would have
expected it should be treated exactly within EL.  So I tried the
analogous thing in c using default formatting for floating point
number output.


c program source

// very simple floating point representation test

#include <stdio.h>
#include <stdlib.h>
  
void main()
{
  float foo;
  
  printf( "\nFloating Point Precision Test \n" );
  
  foo = 100.03125;
  printf( "\nValue set is 100.03125 \n" );
  printf( "Floating conversion = %f \n", foo );
  
  foo = 100.03125 * 100.0;
  printf( "\nValue set is 100.03125 * 100.0 \n" );
  printf( "Floating conversion = %f \n", foo );
}


Output from compiling with the last 16-bit version 
of MS Visual C++ (version 1.52):

Floating Point Precision Test

Value set is 100.03125
Floating conversion = 100.031250

Value set is 100.03125 * 100.0
Floating conversion = 10003.125000


All I can say is, EL is worse than it should be.  You have to 
go with the results of Bob's analysis: "The [EL] print routines
do truncate results to less precision than the internal accuracy
for some reason."

Rod