Mathematics · Algorithms · Numerical Analysis
📅 April 2026 ⏱ ≈ 11 min read 🎯 Intermediate

Newton's Method & Square Roots — Quadratic Convergence and the Fast Inverse Sqrt

Newton's method (Newton-Raphson) is one of the most elegant and powerful algorithms in numerical mathematics. Starting from a tangent-line approximation, it refines a root estimate by squaring the number of correct digits with each iteration — quadratic convergence. Applied to the square root function it becomes the ancient Babylonian method. Applied to the inverse square root with a bit-level hack, it becomes the legendary algorithm that powered Quake III Arena's lighting engine.

1. Newton-Raphson Derivation

To find a root of f(x) = 0: start at a guess x₀, draw the tangent line to the curve at (x₀, f(x₀)), and find where it crosses zero.

Tangent line at (xₙ, f(xₙ)): y - f(xₙ) = f'(xₙ)(x - xₙ) Set y = 0: x_{n+1} = xₙ - f(xₙ) / f'(xₙ) This is the Newton-Raphson iteration formula. Geometric interpretation: Replace the curve with its linear approximation (tangent). The x-intercept of the tangent is a better estimate of the root. Repeat until |f(xₙ)| < tolerance. Example: Find √2 using f(x) = x² - 2, f'(x) = 2x x_{n+1} = xₙ - (xₙ² - 2)/(2xₙ) = (xₙ + 2/xₙ)/2 Starting from x₀ = 1: x₁ = (1 + 2)/2 = 1.5 x₂ = (1.5 + 2/1.5)/2 = 1.41667 x₃ = (1.41667 + 2/1.41667)/2 = 1.41421356... x₄ = 1.4142135623730950488... (34 correct digits already)

2. Quadratic Convergence Proof

Let r be the true root: f(r) = 0. Define error: eₙ = xₙ - r Taylor expansion of f around r: f(xₙ) = f(r) + f'(r)eₙ + ½ f''(r) eₙ² + O(eₙ³) = f'(r)eₙ + ½ f''(r) eₙ² (since f(r)=0) f'(xₙ) = f'(r) + f''(r)eₙ + O(eₙ²) Newton step: x_{n+1} = xₙ - f(xₙ)/f'(xₙ) e_{n+1} = x_{n+1} - r = xₙ - r - f(xₙ)/f'(xₙ) = eₙ - [f'(r)eₙ + ½f''(r)eₙ²] / [f'(r) + f''(r)eₙ] = eₙ · [1 - f'(r)/(f'(r)+f''(r)eₙ)] - ½f''(r)eₙ²/f'(r) + ... ≈ f''(r)/(2f'(r)) · eₙ² (to leading order) Therefore: |e_{n+1}| ≈ C · eₙ² where C = |f''(r)/(2f'(r))| This is QUADRATIC convergence: If eₙ = 10⁻³, then e_{n+1} ≈ C × 10⁻⁶ (3 correct digits → 6 → 12 → 24...) The number of correct digits approximately doubles each iteration. Conditions: x₀ near enough to r, f'(r) ≠ 0 (non-degenerate root)

3. Square Root via Newton's Method

To compute √S: solve f(x) = x² - S = 0 f'(x) = 2x Newton step: x_{n+1} = x - (x² - S)/(2x) = (x + S/x) / 2 This is the average of x and S/x. Geometric mean ≤ arithmetic mean → each xₙ ≥ √S (once x₀ > 0) Sequence is monotone decreasing and bounded below → always converges. JavaScript implementation: function sqrt(S, tolerance = 1e-10) { if (S < 0) throw new Error("negative input"); if (S === 0) return 0; let x = S > 1 ? S / 2 : 1; // initial guess while (Math.abs(x * x - S) > tolerance * S) { x = (x + S / x) / 2; } return x; } Convergence on S = 2, x₀ = 1.5: Iter | xₙ | error 0 | 1.5 | 0.0858 1 | 1.416667 | 0.00245 2 | 1.414216 | 8.4e-7 3 | 1.41421356 | 1.0e-13

4. The Babylonian Method

The iteration x_{n+1} = (x + S/x)/2 for square roots was known to the Babylonians around 1700 BCE and is recorded on tablet YBC 7289. The tablet shows √2 ≈ 1.41421296... computed to 6 sexagesimal places (60-base).

Geometric interpretation of Babylonian method: If you have a rectangle of area S with side lengths x and S/x, the arithmetic mean (x + S/x)/2 is the side of a square with nearly the same area — and is always greater than (or equal to) the square root by AM-GM inequality. Each step produces a less-elongated rectangle converging to a square.

Heron of Alexandria (c. 50 CE) described the same algorithm and is sometimes credited with it in Western tradition. The method is also called "Heron's method." It's the oldest known example of an iterative numerical algorithm.

5. The Quake III Fast Inverse Square Root

In 1999, id Software's Quake III Arena contained a function Q_rsqrt that computed 1/√x ≈ 1.6% accurate in just a few operations — no division, no sqrt call. It was needed for lighting normal calculations (normalizing vectors requires dividing by their length).

// Original Quake III source code (C), circa 1999
// Attributed to John Carmack; magic constant likely from Cleve Moler / Greg Walsh
float Q_rsqrt(float number) {
    long i;
    float x2, y;
    const float threehalfs = 1.5F;
    x2 = number * 0.5F;
    y  = number;
    i  = *(long *)&y;            // bit-reinterpret float as integer
    i  = 0x5f3759df - (i >> 1);   // magic constant initial guess
    y  = *(float *)&i;            // bit-reinterpret integer back to float
    y  = y * (threehalfs - (x2 * y * y));  // ONE Newton-Raphson iteration
    return y;
}

The key insight: IEEE 754 floating point represents a number as (1+m)×2^e in memory. The bit representation is (e + 127) in the high bits and m in the low bits. So log₂(x) ≈ i/L − 127 where i is the integer bit pattern and L = 2²³. The right-shift (i >> 1) approximates dividing log₂(x) by 2 — which in normal space divides the exponent by 2, i.e., takes square root. Subtracting from the magic constant flips the result to 1/√x. One Newton-Raphson iteration (for f(y) = 1/y² − x = 0) gives y = y(3/2 − x/2 · y²), refining to ~1.6% error.

Modern relevance: Modern CPUs and GPUs have hardware sqrt and rsqrt instructions. The Quake hack has been superseded, but the magic constant 0x5f3759df remains one of the most famous numbers in game development history.

6. When Newton's Method Fails

7. Higher-Order Methods

Secant Method (no derivative required): x_{n+1} = xₙ - f(xₙ)·(xₙ - x_{n-1})/(f(xₙ) - f(x_{n-1})) Convergence order: φ ≈ 1.618 (golden ratio) — super-linear but sub-quadratic. Halley's Method (cubic convergence): x_{n+1} = xₙ - 2f(xₙ)f'(xₙ) / (2[f'(xₙ)]² - f(xₙ)f''(xₙ)) Requires f''; tripling correct digits per step. Brent's Method: Combines bisection (guaranteed, slow) + secant method (fast in smooth regions) Robust fallback: the go-to algorithm in LAPACK, GNU GSL, Python scipy. For square root specifically: Goldschmidt's algorithm: Scale x to [0.5, 1]; use lookup for initial estimate y₀ ≈ 1/√x Then iterate: r_{n+1} = (3 - xₙ)/2, xₙ₊₁ = xₙ·rₙ² Quadratic convergence, designed for hardware pipeline. Used in IBM POWER processor sqrt unit.
📐 Explore Mathematics →