Download MetaTrader 5

strange math inside MT5 - double result = 20/40 = 0.0 ?!?

To add comments, please log in or register
Grzegorz Korycki
53335
Grzegorz Korycki 2014.08.06 10:45 

I was getting strage results of my math, when i isolated the problem:

How can double result of 20/40 can be 0.0 ? What am I doing wrong?


wtf

Grzegorz Korycki
53335
Grzegorz Korycki 2014.08.06 11:04  

i managed to find workaround. This is absurd though!

If those were the times when we were still using 16mhz processors that useless equation would take a lot of cpu power. (dividing by 10 is time consuming, 10 != 2^n or sum of those divisions for different n)


workaround

Uwe Goetzke
1483
Uwe Goetzke 2014.08.06 11:25  

20/40 is calculated as integer values. So the result of 0  is an integer assigned to a double value.

20.0/40 is calculated as double values. So the result of 0.5  is an double assigned to a double value.

Irwan Adnan
925
Irwan Adnan 2014.08.06 11:46  

I made a little experiment and found that it is related to data type. You have to put decimal sign it est 20./40. or you may use 

 

double a = 20,
       b = 40;

void OnStart()
{
co_multi = a/b;

Alert ("co_multi = ", co_multi);
}

//then the result is correct.
Grzegorz Korycki
53335
Grzegorz Korycki 2014.08.06 12:23  
 I see now. Thanks for your replies.
To add comments, please log in or register