Wrong computation

 
int maximumLossPerTrade;
int accountBalanceinEuro;
double maximumLossPercentage;

maximumLossPerTrade = (int)(accountBalanceinEuro / 100 * NormalizeDouble(maximumLossPercentage, 1));

When;

accountBalanceinEuro = 14232 
maximumLossPercentage = 1.8

I get 255 for maximumLossPerTrade.


But, in excel, it is 256 which is the correct value.


What is wrong in the above MQL 4 computation ?

 

 typecasting error

maximumLossPerTrade = accountBalanceinEuro / 100 * NormalizeDouble(maximumLossPercentage, 1);
 
paul selvan:

 typecasting error


Without typecasting, it again gives 255.

 
int maximumLossPerTrade;
int accountBalanceinEuro;
 accountBalanceinEuro / 100
14232/100=142, 142*1.8=255
          On MT4 v434, division quotient don't give floating point values(Bug??) - MQL4 and MetaTrader 4 - MQL4 programming forum 2012.09.18
 


Solved it in this way:

maximumLossPerTrade = (int)((accountBalanceinEuro * NormalizeDouble(maximumLossPercentage, 1)) / 100);

Thanks William.

 

... or like so of course :)

maximumLossPerTrade = (int)(accountBalanceinEuro / 100.0 * NormalizeDouble(maximumLossPercentage, 1));
 

setting all variables as double gives good precision

double accountBalanceinEuro = 14232;
double maximumLossPercentage = 1.8;
double maximumLossPerTrade = accountBalanceinEuro * maximumLossPercentage/ 100 ;
 
paul selvan:

setting all variables as double gives good precision


Of course, thanks.