Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Possible issue with the nthRoot() function #490
After passing the expressions below to the MathJS.eval() function, I get the exception shown below thrown back.
Is the expression that I am passing in to the MathJS.eval() function incorrect?
// due to "even" and "odd" roots laying close to each other, // and limits in the floating number precision, // negative bases with fractional exponents always return NaN Math.pow(-7, 1/3); // NaN
In this particular case the algorithm evaluates the following, resulting in NaN:
Math.pow(-46.209093934213584, -0.9816843611112658); // NaN
So we have to see if we can improve
referenced this issue
Oct 29, 2015
I submitted a pull request #496 which replaces the algorithm with this:
// x = a ^ (1/root) var x = Math.pow(Math.abs(a), 1/root); // If a is negative and root is odd, then (-1) ^ (1/root) = -1 x = a < 0 ? -x : x;
The square root algorithm (and cube root, etc.) is one of my favorite algorithms. The algorithm comes from Newton's method, which converges quickly but occasionally runs into problems.
I thought that perhaps the algorithm may be faster than my suggestion above when root is an integer, since
Even when using my own "optimized" version of