[SOLVED] Zero division error, but how?

 

I'm getting some zero-division errors in my code and I don't even know why, or how. I have tried checking the values in every possible way:



How could that even be possible? My program is a bit heavy (and probably that is where the cause is), but even then, what could make the value pass the first condition and give error?

 
ExtMBuffer is not the same as ExtCCIBuffer.
 
Marco vd Heijden:
ExtMBuffer is not the same as ExtCCIBuffer.

Obviously not, but that shouldn't affect in the error since it's the dividend


The indicator code is based on this one, with very slight modifications: https://www.mql5.com/en/code/18

Commodity Channel Index (CCI)
Commodity Channel Index (CCI)
  • www.mql5.com
Commodity Channel Index (CCI) measures the deviation of the commodity price from its average statistical price.
 
if (zeroSupposedly == 0.0 || ExtMBuffer == 0)
 
Marco vd Heijden:

It keeps appearing with that check too

Other thing that has me thinking is that it doesn't appear always...

 

Im sorry i made a mistake.

if (zeroSupposedly == 0.0 || ExtMBuffer[i] == 0)
This will only be truth when both values != differ 0
 
Marco vd Heijden:

Im sorry i made a mistake.

This will only be truth when both values != differ 0
Yes I used this one, the other would give compilation error probably
 

Im sorry i meant to say false not true.

If you need true then it would be like this:

if (zeroSupposedly != 0.0 && ExtMBuffer[i] != 0)
 
Manuel Alejandro Cercos Perez: I'm getting some zero-division errors in my code and I don't even know why, or how. I have tried checking the values in every possible way: How could that even be possible? My program is a bit heavy (and probably that is where the cause is), but even then, what could make the value pass the first condition and give error?

Don't use "==" or "!=" on doubles. Its untrustworthy. Try this (untested) and see what you get ...

double zeroSupposedly  = ExtDBuffer[i];
       ExtCCIBuffer[i] = ( zeroSupposedly < 0 ) || ( zeroSupposedly > 0 )
                       ? ExtMBuffer[i] / zeroSupposedly
                       : 0;

By the way, the value of ExtMBuffer[i] has noting to do with zero-divide error, be it a 0 or not. Only zeroSupposedly can cause that.

 
Fernando Carreiro:

Don't use "==" or "!=" on doubles. Its untrustworthy. Try this (untested) and see what you get ...

By the way, the value of ExtMBuffer[i] has noting to do with zero-divide error, be it a 0 or not. Only zeroSupposedly can cause that.

Thank you very much, this seems to work fine

 
Manuel Alejandro Cercos Perez: Thank you very much, this seems to work fine
You are welcome!
Reason: