double trouble

 

Using MetaEditor v 5.00 build 934 and MetaTrader 4.00 build 670 produces the following:

double d1=20/1200*600;
double d2=600*20/1200;
Print("d1=",d1," d2=",d2); // d1=0.0 d2=10.0
 

You are doing integer calculations, the result of which is typecast to a double.  See Typecasting.

The following is a set of examples from the above link:

int    i=1/2;        // no types casting, the result is 0
Print("i = 1/2  ",i);
 
int k=1/2.0;         // the expression is cast to the double type,
Print("k = 1/2  ",k);  // then is to the target type of int, the result is 0
 
double d=1.0/2.0;    // no types casting, the result is 0.5
Print("d = 1/2.0; ",d);
 
double e=1/2.0;      // the expression is cast to the double type,
Print("e = 1/2.0; ",e);// that is the same as the target type, the result is 0.5
 
double x=1/2;        // the expression of the int type is cast to the double target type,
Print("x = 1/2; ",x);  // the result is 0.0

 

In first line of your code, the result of 20/1200 is zero (0) due to integer division; zero (0) multiplied by 600 results in zero (0).  In the second line, 600 multipled by 20 is 12000; that result (12000) divided by 1200 equals 10. 

Change one of your operands into a double type:

double d1=20.0/1200*600;
double d2=600*20.0/1200;
Print("d1=",d1," d2=",d2); // d1=10.0 d2=10.0

Accordingly, all operands are cast to double which produces the expected result.

 
Thank you very much Thirteen. Take care, Teddy.