Solving quadratics with irrational solutions fails when a > 1
nicolewhite opened this issue · 0 comments
nicolewhite commented
This is in reference to #37.
Solving quadratics with irrational solutions where a > 1 fails due to an order of operations fail.
This, from the source:
var root1 = (-b - squareRootDiscriminant) / 2*a;
var root2 = (-b + squareRootDiscriminant) / 2*a;
Should be:
var root1 = (-b - squareRootDiscriminant) / (2*a);
var root2 = (-b + squareRootDiscriminant) / (2*a);
The test for this didn't catch it because the test had a = 1:
it("should return two numbers if the equation has two real roots that are irrational", function() {
var ex = x.multiply(x).add(x.multiply(4)).add(2);
var eq = new Equation(ex, 0); // x^2 + 4x + 2 = 0
var answers = eq.solveFor("x"); // -2 - √2, √2 - 2
var expected = [-2 - Math.sqrt(2), Math.sqrt(2) - 2];
expect(round(answers[0])).toEqual(round(expected[0]));
expect(round(answers[1])).toEqual(round(expected[1]));
});