1 / 2 = 0 What is wrong ???

 

After hours spended to debug a code, I finally found this bug :

int I = 2;
double D = (I - 1) / I;

Print("D = ", DoubleToStr(D, 8));

Result is 0.000000

(its really 0, as 1/D returns a zerodivide error)

So, please, what is wrong ??

MT4 build 220, Vista 64

Thank you very much

 

Use

double I = 2;
double D = (I - 1) / I;

or

int I = 2;
double t = I;
double D = (t - 1) / t;
 
Rosh :

Use

double I = 2;
double D = (I - 1) / I;

or

int I = 2;
double t = I;
double D = (t - 1) / t;

Thank you Rosh for your fast reply

The first solution is not universal, sometime variables must be integer like arrays indices, period or shift of indicators.

The second one seems a working workaround.

I knew that there is no implicit casting for assignements, but I still dont understand why the inplicit casting is not working only for the division...

Anyway, I will survive, Thanks again