Simplify Numerical.solveQuadratic()

This commit is contained in:
Jürg Lehni 2013-10-18 11:48:23 +02:00
parent a07538b205
commit 89704243b1

View file

@ -138,16 +138,17 @@ var Numerical = new function() {
// If all the coefficients are 0, we have infinite solutions! // If all the coefficients are 0, we have infinite solutions!
return abs(c) < epsilon ? -1 : 0; // Infinite or 0 solutions return abs(c) < epsilon ? -1 : 0; // Infinite or 0 solutions
} }
var q = b * b - 4 * a * c; // Convert to normal form: x^2 + px + q = 0
if (q < 0) var p = b / (2 * a);
return 0; // 0 solutions var q = c / a;
q = sqrt(q); var p2 = p * p;
a *= 2; // Prepare division by (2 * a) if (!abs(p2 - q) < epsilon && p2 < q)
var n = 0; return 0;
roots[n++] = (-b - q) / a; var s = p2 > q ? sqrt(p2 - q) : 0;
if (q > 0) roots[0] = s - p;
roots[n++] = (-b + q) / a; if (s > 0)
return n; // 1 or 2 solutions roots[1] = -s - p;
return s > 0 ? 2 : 1;
}, },
/** /**