Skip to content

squareRoot(6) returns 3, squareRoot(12) returns 4; test expects squareRoot(3, 1) = 1.8 (correct: 1.7) #2231

Open

Description

@devladpopov

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(6);     // 3     expected 2   (√6 = 2.449)
squareRoot(12);    // 4     expected 3   (√12 = 3.464)
squareRoot(3, 1);  // 1.8   expected 1.7 (√3 = 1.732)

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.


Found during an independent property-based audit of this repo (full report and reproducible harness: https://github.com/devladpopov/algorithm-autopsy/tree/main/autopsy-02).

Activity

  1. duongynhi000005-oss commented on Oct 30, 2026

    @duongynhi000005-oss

    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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions