Strange maths behaviour

 

A strange behaviour :

    int    Cur_EffSum, Cur_StratEff;   
           
    double Cur_BKPercent; 
    
    Cur_StratEff = 75;
    Cur_EffSum   = 190;
    
    Cur_BKPercent = Cur_StratEff / Cur_EffSum * 100;
    Print("Cur_BKPercent: ", DoubleToStr(Cur_BKPercent,8));

output :

Cur_BKPercent: 0

But this :

    int    Cur_EffSum, Cur_StratEff;   
           
    double Cur_BKPercent; 
    
    Cur_StratEff = 75;
    Cur_EffSum   = 190;
    
    Cur_BKPercent = Cur_StratEff * 100 / Cur_EffSum;
    Print("Cur_BKPercent: ", DoubleToStr(Cur_BKPercent,8));

output :

Cur_BKPercent: 39.47368421

I guess it's because it's first divided and then it's too small, so reduced to 0, before it's multiplied by 100. But if it's first multiplied by 100 then it's big enough. Maybe it's because the first 2 variables are integers, so it rounded it to an integer.
Strange though :)

 

I think it is because you are using integers to do a calculation that would create a double

try it like this

    double    Cur_EffSum, Cur_StratEff;   
           
    double Cur_BKPercent; 
    
    Cur_StratEff = 75;
    Cur_EffSum   = 190;
    
    Cur_BKPercent = Cur_StratEff / Cur_EffSum * 100;
    Print("Cur_BKPercent: ", DoubleToStr(Cur_BKPercent,8));
 

another trick, when wanting to use integers ('cos they display real nice!) is

double myVal = (1.0 * int1) / int 2; //convert to double before doing integer division

Reason: