Algorithm Search⌘K

Proofs: Newton-Raphson division

These are the human proofs of Newton-Raphson division, at full length.

The Newton reciprocal is exact after a bounded correction, and the quotient is off by at most one full

This proof shows: correctness, termination.

For an integer D≥1D \ge 1 let B=B(D)B = B(D) be its binary length, so 2B−1≤D<2B2^{B-1} \le D < 2^B, and let R∗=4B/DR^* = 4^B / D, a real number. Then 2B<R∗≤2B+12^B < R^* \le 2^{B+1}.

Theorem. RECIPROCAL(D)(D) terminates and returns ⌊4B/D⌋\lfloor 4^B/D \rfloor; the loop of line L7 runs at most 16 times in each call. For 0≤U<D2B0 \le U < D 2^B and R=⌊4B/D⌋R = \lfloor 4^B/D \rfloor, DIVIDE(U,D,R)(U, D, R) terminates and returns (⌊U/D⌋,U mod D)(\lfloor U/D \rfloor, U \bmod D), and the loop of line L10 runs at most once.

1. Termination of the recursion. If B>B0≥1B > B_0 \ge 1, then h=⌈B/2⌉h = \lceil B/2 \rceil satisfies 1≤h<B1 \le h < B, and Dh=⌊D/2B−h⌋D_h = \lfloor D / 2^{B-h} \rfloor has binary length exactly hh, since 2B−1/2B−h=2h−12^{B-1}/2^{B-h} = 2^{h-1} and D/2B−h<2hD / 2^{B-h} < 2^h. So each call recurses once on a divisor of strictly smaller length, and the recursion ends at line L1 after at most ⌈log⁡2B⌉\lceil \log_2 B \rceil levels. Line L1 is correct by definition. We now prove a call at length B>B0B > B_0 correct, assuming that the recursive call returns Xh=⌊4h/Dh⌋X_h = \lfloor 4^h / D_h \rfloor (induction on BB).

2. The start. Let X0=Xh2B−hX_0 = X_h 2^{B-h}, the value of line L4, and t=2B−ht = 2^{B-h}. Then −t<X0−R∗<4t-t < X_0 - R^* < 4t.

Proof. Put d′=D/td' = D / t, a real number with Dh≤d′<Dh+1D_h \le d' < D_h + 1. Then R∗/t=4B/(Dt)=2B+h/D=4h/d′R^*/t = 4^B/(Dt) = 2^{B+h}/D = 4^h/d', since d′=D/2B−hd' = D/2^{B-h}. Now 4h/Dh−1<Xh≤4h/Dh4^h/D_h - 1 < X_h \le 4^h / D_h, and

0≤4hDh−4hd′=4h(d′−Dh)Dhd′<4hDh2≤4h4h−1=4,0 \le \frac{4^h}{D_h} - \frac{4^h}{d'} = \frac{4^h (d' - D_h)}{D_h d'} < \frac{4^h}{D_h^2} \le \frac{4^h}{4^{h-1}} = 4,

using Dh≥2h−1D_h \ge 2^{h-1}. Hence −1<Xh−4h/d′<4-1 < X_h - 4^h/d' < 4, and multiplying by tt gives the claim. □\square

3. The Newton step squares the residual. For any real XX, let E=4B−DXE = 4^B - DX and X∗=X+XE/4BX^* = X + XE/4^B. Then

4B−DX∗=4B−DX−DX⋅E4B=E−(4B−E)E4B=E24B≥0,4^B - DX^* = 4^B - DX - \frac{DX \cdot E}{4^B} = E - \frac{(4^B - E)E}{4^B} = \frac{E^2}{4^B} \ge 0 ,

so X∗≤R∗X^* \le R^* and R∗−X∗=E2/(4BD)R^* - X^* = E^2/(4^B D). □\square

4. After line L6. Let X1X_1 be the value after line L6. Then R∗−17<X1≤⌊R∗⌋R^* - 17 < X_1 \le \lfloor R^* \rfloor.

Proof. With X=X0X = X_0, line L5 computes the integer E=4B−DX0=D(R∗−X0)E = 4^B - D X_0 = D(R^* - X_0). If E≥0E \ge 0, line L6 sets X1=X0+⌊X0E/4B⌋X_1 = X_0 + \lfloor X_0 E / 4^B \rfloor; if E<0E < 0, it sets X1=X0−⌈X0∣E∣/4B⌉X_1 = X_0 - \lceil X_0 |E| / 4^B \rceil. In both cases X1=⌊X∗⌋X_1 = \lfloor X^* \rfloor where X∗=X0+X0E/4BX^* = X_0 + X_0 E/4^B, because X0X_0 is an integer and −⌈y⌉=⌊−y⌋-\lceil y \rceil = \lfloor -y \rfloor. By part 3, X∗≤R∗X^* \le R^*, so the integer X1≤⌊R∗⌋X_1 \le \lfloor R^* \rfloor. By part 2, ∣E∣<4Dt|E| < 4 D t, so by part 3

R∗−X∗=E24BD<16D2t24BD=16D 4B−h4B=16D4h<16,R^* - X^* = \frac{E^2}{4^B D} < \frac{16 D^2 t^2}{4^B D} = \frac{16 D \, 4^{B-h}}{4^B} = \frac{16 D}{4^h} < 16,

since D<2B≤22h=4hD < 2^B \le 2^{2h} = 4^h. With X1>X∗−1X_1 > X^* - 1 this gives X1>R∗−17X_1 > R^* - 17. □\square

5. Line L7. Line L7 sets E=4B−DX1=D(R∗−X1)≥0E = 4^B - D X_1 = D(R^* - X_1) \ge 0 and keeps the invariant E=4B−DXE = 4^B - DX while it increments XX and subtracts DD from EE. It stops when 0≤E<D0 \le E < D, that is, when X≤R∗<X+1X \le R^* < X + 1, that is, when X=⌊R∗⌋X = \lfloor R^* \rfloor. Starting from X1≤⌊R∗⌋X_1 \le \lfloor R^* \rfloor, it reaches this value after ⌊R∗⌋−X1\lfloor R^* \rfloor - X_1 steps, an integer with ⌊R∗⌋−X1≤R∗−X1<17\lfloor R^* \rfloor - X_1 \le R^* - X_1 < 17. So it runs at most 16 times and line L8 returns ⌊4B/D⌋\lfloor 4^B/D \rfloor. This completes the induction. □\square

6. The quotient. Let 0≤U<D2B0 \le U < D 2^B and R=⌊R∗⌋R = \lfloor R^* \rfloor, so R∗−1<R≤R∗R^* - 1 < R \le R^*. Line L9 sets q=⌊UR/4B⌋q = \lfloor UR / 4^B \rfloor. From R≤R∗R \le R^*, q≤UR∗/4B=U/Dq \le UR^*/4^B = U/D, so q≤⌊U/D⌋q \le \lfloor U/D \rfloor. From R>R∗−1R > R^* - 1 and U<D2B≤4BU < D 2^B \le 4^B (as D<2BD < 2^B),

q>UR4B−1>UD−U4B−1>UD−2≥⌊UD⌋−2,q > \frac{UR}{4^B} - 1 > \frac{U}{D} - \frac{U}{4^B} - 1 > \frac{U}{D} - 2 \ge \Big\lfloor \frac{U}{D} \Big\rfloor - 2 ,

so the integer q≥⌊U/D⌋−1q \ge \lfloor U/D \rfloor - 1. Then r=U−qDr = U - qD satisfies 0≤r<2D0 \le r < 2D, and line L10 subtracts DD at most once, keeping U=qD+rU = qD + r, and stops with 0≤r<D0 \le r < D. By the uniqueness of division with remainder, q=⌊U/D⌋q = \lfloor U/D \rfloor and r=U mod Dr = U \bmod D. □\square

7. Long dividends. DIVIDE on blocks keeps the invariant that, after the blocks above position jj are processed, qq and rr are the quotient and remainder of the number formed by those blocks; each block step has U=r2B+block<D2BU = r 2^B + \text{block} < D 2^B since r<Dr < D, so part 6 applies. This is the invariant of long division with digits of BB binary digits. ■\blacksquare

Back to Newton-Raphson division