Skip to content

Commit

Permalink
Add divide-by-zero check in quadratic regression formula
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin D. Weinberg committed Dec 26, 2024
1 parent dd2b6cd commit 11012a6
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions include/QuadLS.H
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ private:
double Sx2y = sumx2y - sumx2*sumy/n;
double Sx2x2 = sumx4 - sumx2*sumx2/n;

_a = (Sx2y*Sxx - Sxy*Sxx2 ) / (Sxx*Sx2x2 - Sxx2*Sxx2);
_b = (Sxy*Sx2x2 - Sx2y*Sxx2) / (Sxx*Sx2x2 - Sxx2*Sxx2);
_c = (sumy - sumx2*_a - sumx*_b) / n;
double denom = Sxx*Sx2x2 - Sxx2*Sxx2;

if (fabs(denom)>0.0) {
_a = (Sx2y*Sxx - Sxy*Sxx2 ) / denom;
_b = (Sxy*Sx2x2 - Sx2y*Sxx2) / denom;
_c = (sumy - sumx2*_a - sumx*_b) / n;
} else _a = _b = _c = 0.0;

}
else {
_a = _b = _c = 0.0;
Expand Down

0 comments on commit 11012a6

Please sign in to comment.