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.
I investigated #2231 and traced the root cause in src/algorithms/math/square-root/squareRoot.js:
Root Cause
In squareRoot.js:31, the stopping condition Math.abs(number - (root ** 2)) > requiredDelta bounds the residual of the square ($|x^2 - n| \le 10^{-\text{tolerance}}$) rather than the error of the root itself ($|x - \sqrt{n}| \approx \frac{|x^2 - n|}{2\sqrt{n}}$). Because Newton-Raphson converges monotonically from above after the first step, terminating when $x^2 - n \le 1$ (for tolerance = 0) stops as soon as $x \le \sqrt{n + 1}$:
For number = 6, iteration stops at 2.607 ($2.607^2 = 6.798 \le 7$), which rounds to 3 instead of 2 ($\sqrt{6} \approx 2.449$).
For number = 12, iteration stops at 3.550 ($3.550^2 = 12.60 \le 13$), which rounds to 4 instead of 3 ($\sqrt{12} \approx 3.464$).
For number = 3, tolerance = 1, iteration stops at 1.75 ($1.75^2 = 3.0625 \le 3.1$), which rounds to 1.8 instead of 1.7 ($\sqrt{3} \approx 1.732$).
Proposed Fix
Scale the stopping threshold by 2 * Math.max(1, Math.sqrt(number)) (or bound the Newton-Raphson step delta directly) before rounding to tolerance decimal places, and update squareRoot.test.js (squareRoot(3, 1) -> 1.7, plus squareRoot(6) -> 2 and squareRoot(12) -> 3).
I have prepared a clean branch (duongynhi000005-oss/javascript-algorithms:fix/issue-2231-squareroot-convergence-criterion) and would be happy to submit a PR!
Activity
fix(squareRoot): bound root step convergence rather than squared resi…
duongynhi000005-oss commentedon Oct 30, 2026
I investigated #2231 and traced the root cause in
src/algorithms/math/square-root/squareRoot.js:Root Cause
In$|x^2 - n| \le 10^{-\text{tolerance}}$ ) rather than the error of the root itself ($|x - \sqrt{n}| \approx \frac{|x^2 - n|}{2\sqrt{n}}$ ). Because Newton-Raphson converges monotonically from above after the first step, terminating when $x^2 - n \le 1$ (for $x \le \sqrt{n + 1}$ :
squareRoot.js:31, the stopping conditionMath.abs(number - (root ** 2)) > requiredDeltabounds the residual of the square (tolerance = 0) stops as soon asnumber = 6, iteration stops at2.607(3instead of2(number = 12, iteration stops at3.550(4instead of3(number = 3, tolerance = 1, iteration stops at1.75(1.8instead of1.7(Proposed Fix
Scale the stopping threshold by
2 * Math.max(1, Math.sqrt(number))(or bound the Newton-Raphson step delta directly) before rounding totolerancedecimal places, and updatesquareRoot.test.js(squareRoot(3, 1) -> 1.7, plussquareRoot(6) -> 2andsquareRoot(12) -> 3).I have prepared a clean branch (
duongynhi000005-oss/javascript-algorithms:fix/issue-2231-squareroot-convergence-criterion) and would be happy to submit a PR!