polynomialRoot - Incorrect roots returned for small coefficients
katzenbar opened this issue · comments
Describe the bug
When the coefficients are small (~1e-5, with epsilon set to the default 1e-12), polynomialRoot
will return the wrong roots for cubic polynomials.
To Reproduce
Take the example function (x - 1)(x - 2)(x - 3)
, and then multiply it by different powers of 10. This should always return the same roots, but it starts returning different results when multiplying by 1e-5 or smaller.
const baseCoeff = [-6, 11, -6, 1];
math.polynomialRoot(...baseCoeff);
// => [ 1, 3, 2 ]
math.polynomialRoot(...baseCoeff.map((c) => 1e-4 * c));
// => [ 0.9999999999999983, 2.9999999999999973, 2.0000000000000036 ]
math.polynomialRoot(...baseCoeff.map((c) => 1e-5 * c));
// => [ 2.0000000000000235, 1.9999999999999987 ]
Thanks for your inputs @katzenbar.
These results are correct but they contain regular floating point round-off errors.
See docs: https://mathjs.org/docs/datatypes/numbers.html#roundoff-errors