I cannot use double variables correctly. Please help me!

 

Hello friends,

     I'm learning how to code on MT4, however I got stuck at very first line of coding. Here what I have tried to do:

void OnStart()

{

    double s;

    s = 1/2 + 1/3 + 1/4;

    Alert("The result is: ",s);

    return;

}

   Surprisingly, the result I got on the sreen is The result is: 0;

   Please tell me what is happening, and why that happens?

   Thank you!

 
GreatCuong:

Hello friends,

     I'm learning how to code on MT4, however I got stuck at very first line of coding. Here what I have tried to do:

void OnStart()

{

    double s;

    s = 1/2 + 1/3 + 1/4;

    Alert("The result is: ",s);

    return;

}

   Surprisingly, the result I got on the sreen is The result is: 0;

   Please tell me what is happening, and why that happens?

   Thank you!

because you are processing all integers in the expressions

change the 1s to 1.0 and see the difference

so use doubles for the expressions or cast them with (double) 

 
GreatCuong:


   Surprisingly, the result I got on the sreen is The result is: 0;

   Please tell me what is happening, and why that happens?

   Thank you!

double s;

s = 1/2 + 1/3 + 1/4;

Alert("The result is: ",s);

It is better to use DoubleToString() function if you want to print the value of a double variable.