Download MetaTrader 5

Problems with division

To add comments, please log in or register
Cristian Eriksson
890
Cristian Eriksson  

Hallo forum!: First post

Until now I ´ve solved all problems on my own, by googeling. But this as puzzled me for months.


 

I´ve either figuerd ways around it or ignored it. It´s only in some functions it shows up...


   if(AboveCounter>BelowCounter&&AboveCounter>0&& BelowCounter>0) xBias=NormalizeDouble(AboveCounter/BelowCounter,2);
      if(BelowCounter>AboveCounter&&AboveCounter>0&& BelowCounter>0) xBias=NormalizeDouble(BelowCounter/AboveCounter,2);


      Alert("Depth ",xRangeDepth," ",AboveCounter," ",BelowCounter," ",xBias);


Why is xBias only getting round numbers, like 1.0, 2.0, 3.0 when the variables AboveCounter and Belowcounters  can be 18/32, 45/19, 15/27 etc..


If anyone could help me I would be very thankfull!

Cristian Eriksson
890
Cristian Eriksson  

I solved it!!

For some mysterious reason, you can´t divide two ints and get a double.. Hmm, I don´t see the logic in that.


Search words:

Problems with division

Division results in round numbers

Divide int

honest_knave
Moderator
2325
honest_knave  

Try reading this article on typecasting.

The relevant bits are these examples:

Examples:

   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 typr,
   Print("x = 1/2; ",x);  // the result is 0.0

Typecasting - Data Types - Language Basics - MQL4 Reference
Typecasting - Data Types - Language Basics - MQL4 Reference
  • docs.mql4.com
Typecasting - Data Types - Language Basics - MQL4 Reference
To add comments, please log in or register