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
Incorrect results from 'nthRoot' #397
There are numerical instabilities in the
$ ./repl.js > math.eval('sqrt(2^1000)/2^500'); 1 > math.eval('nthRoot(2^1000,2)/2^500'); 2.5822498780869086e+120 >
I haven't looked at the code of
Are there any algorithms that handle those instabilities better?
Thanks! Turns out there is an infinite loop protection with a too low maximum count (here in the code).
Under what conditions would this be an infinite loop? If I understand it correctly, this comes down to the question if there are parameters for which the algorithm doesn't converge (not mathematically but numerically ).
If it's not sure if this converges,
Another suggestion: Throw an error when
I think an error should be thrown because otherwise the result is incorrect and you have no way of knowing that it is.
changed the title from
Numerical instabilities in `nthRoot`
Incorrect results from 'nthRoot'
Jun 17, 2015
added a commit
Jun 17, 2015
I've changed the max iterations to 10000 (it's typically between 5 and 20, and for very small or large values it can be in the order of 500).
The function now throws an error when the maximum iterations is reached. However, so far I couldn't come up with a case where this happens. I think adding an optional third parameter will be a bit overkill, the method should just work.
I also saw that the function didn't work at all for small values ( < 1e-16 ) due to the epsilon. I've fixed that too by comparing x against xPrev in the loop.
The issue can stay open until the issue is applied in the first next release (will automatically be closed by github then). We could also do a bugfix for the v1 branch.