cruncher icon indicating copy to clipboard operation
cruncher copied to clipboard

Wrong NaN in division solving

Open tianshuo opened this issue 11 years ago • 4 comments

15/5=3 Unlock 3, Lock 5, Change 3 to -3, 5 turns into NaN, should be -5

tianshuo avatar Dec 31 '13 06:12 tianshuo

It's an issue with the solver itself; Newton's method diverges with f(x) = 15/x + 3, approximated f', and x_0 = 1 (which is a constant right now). It converges if I pick x_0 in (-1, 0). Not sure what the mathematical reasons for this are. Hacky solution would be to just try both x_0 = -0.01 and 0.01, or something like that, as starting values.

osnr avatar Jan 03 '14 21:01 osnr

Correction: You are actually using Secant's Method (since you're not taking the derivative) How about using the Brent's method instead? http://en.wikipedia.org/wiki/Brent%27s_method

tianshuo avatar Jan 04 '14 07:01 tianshuo

You're right -- my mistake. There is a JS implementation of Brent's method: https://gist.github.com/borgar/3317728

I'll look at it soon; looks like we need to bracket the root first to use that method, though.

osnr avatar Jan 06 '14 19:01 osnr

Still breaks on larger right-side numbers than 3. Actually, this bug wasn't fixed by using Brent's method -- it was fixed by the second x_0 we now test as part of the Brent-vs-secant choosing procedure in findRoot.

We don't have a bracket for Brent's method, so I just made one up -- [-1, 1], which converges to the singularity instead of the root for this function. (It also fails to converge for f(x) = x^2 and a bunch of other simple functions). Are there any smarter ways to come up with a bracket?

osnr avatar Feb 08 '14 21:02 osnr