MT4 forcing doubles into int

 

256 divided by 5 is 51.2, but MT4 gives me 51.00000000.


int OnInit()   {
   Average = (256 / 5);
   drawLabel();
   return(INIT_SUCCEEDED);
}

double Average;

void drawLabel()  {
   string name = "LabelDebug";
   string text = "Average: " + DoubleToString(Average, 8);
   if (ObjectFind(name) != -1) {ObjectDelete(name);}
   ObjectCreate(name, OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText(name, text, 14, "Times New Roman", Black);
   ObjectSet(name, OBJPROP_CORNER, 1);
   ObjectSet(name, OBJPROP_XDISTANCE, 10);
   ObjectSet(name, OBJPROP_YDISTANCE, 60);
}

void RunLoop()   {drawLabel();}

void OnTick()  {RunLoop();}


Why is that?

 
whoowl: 256 divided by 5 is 51.2, but MT4 gives me 51.00000000. Why is that?

You are doing Integer division. Use floating point division by making at least one of the numbers floating point:

Average = (256.0 / 5  );
Average = (256   / 5.0);
Average = (256.0 / 5.0);
 

Forum on trading, automated trading systems and testing trading strategies

Rare code issue

Fernando Carreiro, 2021.07.23 22:20

Please understand that dividing by 100 or dividing by 100.0 will give totally different results.

In the former, 100 is considered an Integer literal constant and so it will carry out integer division which will truncate the decimal values.

In the latter, 100.0 will be considered a floating-point literal constant and it will carry out floating-point division which will keep the decimal values.

This is the principal behind the problems you are having. So, to prevent this, either use a floating-point literal constant or typecast the variable into a (double).

Example:

Print( "90   / 100   = ", 90   / 100   );
Print( "90   / 100.0 = ", 90   / 100.0 );
Print( "90.0 / 100   = ", 90.0 / 100   );
Print( "90.0 / 100.0 = ", 90.0 / 100.0 );
2021.07.23 21:26:57.060 TestCopy EURUSD,H1: 90   / 100   = 0
2021.07.23 21:26:57.060 TestCopy EURUSD,H1: 90   / 100.0 = 0.9
2021.07.23 21:26:57.060 TestCopy EURUSD,H1: 90.0 / 100   = 0.9
2021.07.23 21:26:57.060 TestCopy EURUSD,H1: 90.0 / 100.0 = 0.9

 
Fernando Carreiro #:

You are doing Integer division. Use floating point division by making at least one of the numbers floating point:


OK. But that division was just an example to illustrate the problem. In the actual code, I don't provide the numbers.

For example, a certain number of bars are found   that will meet a certain condition. Then I want to get the average number of bars that meet the condition:


double Average = (Bars / Found);


Both numbers will be integers.

 
whoowl #OK. But that division was just an example to illustrate the problem. In the actual code, I don't provide the numbers. For example, a certain number of bars are found   that will meet a certain condition. Then I want to get the average number of bars that meet the condition: Both numbers will be integers.

Typecast them into floating point — Typecasting - Data Types - Language Basics - MQL4 Reference

double Average = (double) Bars /          Found;
double Average =          Bars / (double) Found;
double Average = (double) Bars / (double) Found;
Typecasting - Data Types - Language Basics - MQL4 Reference
Typecasting - Data Types - Language Basics - MQL4 Reference
  • docs.mql4.com
Typecasting - Data Types - Language Basics - MQL4 Reference
 
It worked. It's solved. Thank you!
 
whoowl #: It worked. It's solved. Thank you!
You are welcome!
Reason: