From 89704243b1bb9d875d4f0e5e2b65a544366d8f7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Fri, 18 Oct 2013 11:48:23 +0200 Subject: [PATCH] Simplify Numerical.solveQuadratic() --- src/util/Numerical.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/util/Numerical.js b/src/util/Numerical.js index ca4a295b..d4fc0364 100644 --- a/src/util/Numerical.js +++ b/src/util/Numerical.js @@ -138,16 +138,17 @@ var Numerical = new function() { // If all the coefficients are 0, we have infinite solutions! return abs(c) < epsilon ? -1 : 0; // Infinite or 0 solutions } - var q = b * b - 4 * a * c; - if (q < 0) - return 0; // 0 solutions - q = sqrt(q); - a *= 2; // Prepare division by (2 * a) - var n = 0; - roots[n++] = (-b - q) / a; - if (q > 0) - roots[n++] = (-b + q) / a; - return n; // 1 or 2 solutions + // Convert to normal form: x^2 + px + q = 0 + var p = b / (2 * a); + var q = c / a; + var p2 = p * p; + if (!abs(p2 - q) < epsilon && p2 < q) + return 0; + var s = p2 > q ? sqrt(p2 - q) : 0; + roots[0] = s - p; + if (s > 0) + roots[1] = -s - p; + return s > 0 ? 2 : 1; }, /**