You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
src/algorithms/math/square-root/squareRoot.js stops Newton's iteration when |number - root²| <= 10^-tolerance (line 31). That bounds the error of the square, not of the root. Newton's method approaches √n from above, so the loop stops on an overestimate, and rounding then goes the wrong way.
squareRoot.test.js:30 asserts expect(squareRoot(3, 1)).toBe(1.8), so the test enshrines the bug. For n = 0..3000 and tolerance 0..6 there are 68 wrong results.
Fix: iterate until the root stops decreasing (while (next < root)), then round. Checked on 259,695 inputs against correctly rounded Math.sqrt; the only mismatches are 53 last-digit float effects at tolerance >= 10. This also removes the hang reported in #2187 (squareRoot(65549, 11)). The #2187 change alone still returns 3 and 1.8.
Activity