From 11012a62eb626ee1d16e83328f9d3b9d2c2850e6 Mon Sep 17 00:00:00 2001 From: "Martin D. Weinberg" Date: Thu, 26 Dec 2024 17:55:05 -0500 Subject: [PATCH] Add divide-by-zero check in quadratic regression formula --- include/QuadLS.H | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/include/QuadLS.H b/include/QuadLS.H index 94474551..1f5d4d36 100644 --- a/include/QuadLS.H +++ b/include/QuadLS.H @@ -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;