I have downloaded a MetaTrader MQL4
language .mq4
source-code file from here and I think there is a divide by zero error contained in the file. The relevant section is:
// Calculate sums for the least-squares method
n = ( Taps - 1 ) / 2;
sx2 = ( 2*n + 1 ) / 3.0;
sx3 = n * ( n + 1 ) / 2.0;
sx4 = sx2 * ( 3*n*n + 3*n - 1 ) / 5.0;
sx5 = sx3 * ( 2*n*n + 2*n - 1) / 3.0;
sx6 = sx2 * ( 3*n*n*n*( n + 2 ) - 3*n + 1 ) / 7.0;
den = sx6 * sx4 / sx5 - sx5; // <---------------------------- a DIV!0 error here?
Am I correct in my assumption that there is an error in the code,
and if so,
perhaps someone could point out what the correction should be?
What is the industry best-practice / what practical software-design measures ought be used as a life-jacket protection for DIV!0
incident(s)?
A division by zero will occur if sx5 is zero. To find what would cause sx5 to be zero, solve sx5=0 for n.
0 = sx5
0 = sx3 * (2*n*n + 2*n - 1) / 3.0
0 = (n*(n + 1) / 2.0) * (2*n*n + 2*n - 1) / 3.0
...
0 = 2*n^4 + 4*n^3 + n^2 - n
One possible solution to that equation is n=0, so a division by zero error will occur if Taps is 1. I don't know if the equation has other solutions.
Update: Added math.