How solve this zero divide error in Trend calculation

 

I found the zero divide error in this trend line calculation

int fnGetTrendLineSignal(string strName, int type)
{
// get the four coordinates
double x1 = ObjectGet( strName, OBJPROP_TIME1);
double y1 = ObjectGet( strName, OBJPROP_PRICE1);
double x2 = ObjectGet( strName, OBJPROP_TIME2);
double y2 = ObjectGet( strName, OBJPROP_PRICE2);

// calculate the slope of the line (m)
if( (x2-x1) != 0)
Print(GetLastError());
double m = (y2-y1)/(x2-x1);

// calcualte the offset (b)
double b = y1 -m*x1;

// get the current x value
double time = TimeCurrent();

// calculate the value (y) of the projected trendline at (x): y = mx + b
double value = m*time + b;

// if type is an upper trend line (line is above price points)
if( type == 0 )
{
// price has broken the trendline
// the bid price is higher than the projected value
if( Bid > value )
   return(OP_BUY);
}

// if type an is lower trend line (line is below price points)
if( type == 1 )
{
// price has broken the trendline
// the ask price is lower than the projected value
if( Ask < value )
return(OP_SELL);
}

return(-1);
}

How can solve this error

if( (x2-x1) != 0)
Print(GetLastError());
double m = (y2-y1)/(x2-x1);

I have no experience in this zero divide error.

Can someone guide me .

How can be ok in this trend calculation.

thanks

 

I'd probably do something like this:

if( (x2-x1) == 0)
{
        Print("Divide by zero!");
        return -1;
}
 
Anthony Garot:

I'd probably do something like this:

yes it is work but how can get trend value from this

// calculate the slope of the line (m)
if( (x2-x1) == 0)
{
        Print("Divide by zero!");
        return -1;
}
double m = (y2-y1)/(x2-x1);


trend not still work .

how can get the value to get trend bro.

 

"Bro", this isn't even your own code (https://www.mql5.com/en/forum/136935). If you reuse other people's code, make sure it's at least working.

Sorry for being the a-hole here, but if you didn't put any original thought into it and don't understand what this code is doing, any help is pointless.

 



double m;
if
( x2!= 0 && x1!= 0 && y2!= 0 && y1!=0
) m = (y2-y1)/(x2-x1);
/* Print(GetLastError());
  double m = (y2-y1)/(x2-x1); */
 

It is necessary to check: is the divisor equal to zero?

   double m=0.0;
   //--- is the divisor equal to zero?
   if((x2-x1)!=0)
      m =(y2-y1)/(x2-x1);