Algorithm Search⌘K

Proofs: Goldschmidt division

These are the human proofs of Goldschmidt division, at full length.

Goldschmidt division with a final exact correction returns the correctly rounded quotient full

This proof shows: correctness.

Setting. The algorithm of the page, for positive normal binary64 numbers a=A⋅2eaa = A \cdot 2^{e_a} and b=B⋅2ebb = B \cdot 2^{e_b} with A,B∈[1,2)A, B \in [1, 2) and a normal quotient (signs are handled separately and are exact). The iteration works with integers scaled by 2−W2^{-W}, W=64W = 64; every product is truncated toward zero to WW fractional bits. Write tr(x)\mathrm{tr}(x) for this truncation, so 0≤x−tr(x)<2−W0 \le x - \mathrm{tr}(x) < 2^{-W} for x≥0x \ge 0. The seed f0f_0 is 1/ci1/c_i rounded to 1010 fractional bits, where ci=1+(i+12)/27c_i = 1 + (i + \tfrac12)/2^{7} and i=⌊(B−1)27⌋i = \lfloor (B - 1) 2^{7} \rfloor. The number of iterations is k≥3k \ge 3. Let Q=A/B∈(12,2)Q = A/B \in (\tfrac12, 2) and let v=2−52v = 2^{-52} if A≥BA \ge B and v=2−53v = 2^{-53} otherwise, so that vv is the spacing of binary64 numbers at QQ.

Theorem. The returned value is RN(a/b)\mathrm{RN}(a/b), the binary64 number nearest to a/ba/b.

Lemma 1 (seed). ∣1−Bf0∣<2−7|1 - Bf_0| < 2^{-7}.

Proof. ∣B−ci∣≤2−8|B - c_i| \le 2^{-8} and ci≥1c_i \ge 1, so ∣1−B/ci∣≤2−8|1 - B/c_i| \le 2^{-8}. Rounding gives ∣f0−1/ci∣≤2−11|f_0 - 1/c_i| \le 2^{-11}, so ∣Bf0−B/ci∣≤2⋅2−11|B f_0 - B/c_i| \le 2 \cdot 2^{-11}. Hence ∣1−Bf0∣≤2−8+2−10<2−7|1 - Bf_0| \le 2^{-8} + 2^{-10} < 2^{-7}. ■\blacksquare

Lemma 2 (convergence with truncation). Let dj=1−ηjd_j = 1 - \eta_j and nj=Qdj+τjn_j = Q d_j + \tau_j after line L3 (j=0j = 0) and after the jj-th pass of lines L5 and L6. Then ∣η0∣<2−7+2−64|\eta_0| < 2^{-7} + 2^{-64}, 0≤ηj+1<ηj2+2−W0 \le \eta_{j+1} < \eta_j^2 + 2^{-W}, and ∣τj∣<(j+1)⋅21−W(1+2−6)j|\tau_j| < (j + 1) \cdot 2^{1-W} (1 + 2^{-6})^{j}.

Proof. Line L3 gives d0=Bf0−β0d_0 = Bf_0 - \beta_0 and n0=Af0−α0n_0 = Af_0 - \alpha_0 with 0≤α0,β0<2−W0 \le \alpha_0, \beta_0 < 2^{-W}, so η0=(1−Bf0)+β0\eta_0 = (1 - Bf_0) + \beta_0, which with Lemma 1 gives the first claim, and τ0=n0−Qd0=Qβ0−α0\tau_0 = n_0 - Qd_0 = Q\beta_0 - \alpha_0, so ∣τ0∣<2⋅2−W|\tau_0| < 2 \cdot 2^{-W} as Q<2Q < 2.

Line L5 sets fj=2−dj=1+ηjf_j = 2 - d_j = 1 + \eta_j exactly. Line L6 gives dj+1=djfj−β=(1−ηj)(1+ηj)−β=1−ηj2−βd_{j+1} = d_j f_j - \beta = (1 - \eta_j)(1 + \eta_j) - \beta = 1 - \eta_j^2 - \beta, so ηj+1=ηj2+β∈[0,ηj2+2−W)\eta_{j+1} = \eta_j^2 + \beta \in [0, \eta_j^2 + 2^{-W}). It also gives nj+1=njfj−α=Qdjfj+τjfj−α=Q(dj+1+β)+τjfj−αn_{j+1} = n_j f_j - \alpha = Q d_j f_j + \tau_j f_j - \alpha = Q(d_{j+1} + \beta) + \tau_j f_j - \alpha, so τj+1=τjfj+Qβ−α\tau_{j+1} = \tau_j f_j + Q\beta - \alpha and ∣τj+1∣<∣τj∣(1+∣ηj∣)+21−W|\tau_{j+1}| < |\tau_j|(1 + |\eta_j|) + 2^{1-W}. Since ∣ηj∣<2−6|\eta_j| < 2^{-6} for all jj, induction gives the bound on τj\tau_j. ■\blacksquare

Lemma 3 (accuracy after three iterations). For 3≤k≤643 \le k \le 64, ∣nk−Q∣<2−54|n_k - Q| < 2^{-54}.

Proof. From Lemma 2, η1<(2−7+2−64)2+2−64<2−13.9\eta_1 < (2^{-7} + 2^{-64})^2 + 2^{-64} < 2^{-13.9}, η2<2−27.8+2−64<2−27.7\eta_2 < 2^{-27.8} + 2^{-64} < 2^{-27.7} and η3<2−55.4+2−64<2−55.3\eta_3 < 2^{-55.4} + 2^{-64} < 2^{-55.3}; for j≥3j \ge 3, ηj+1<ηj2+2−64<2−55.3\eta_{j+1} < \eta_j^2 + 2^{-64} < 2^{-55.3} as well. For τ\tau, Lemma 2 gives ∣τ3∣<4⋅2−63(1+2−6)3<2−60.9|\tau_3| < 4 \cdot 2^{-63} (1 + 2^{-6})^{3} < 2^{-60.9}, and for j≥3j \ge 3 the recurrence ∣τj+1∣<∣τj∣(1+2−55)+2−63|\tau_{j+1}| < |\tau_j|(1 + 2^{-55}) + 2^{-63} gives ∣τk∣<2−60.9(1+2−49)+61⋅2−63<2−56.9|\tau_k| < 2^{-60.9}(1 + 2^{-49}) + 61 \cdot 2^{-63} < 2^{-56.9} for k≤64k \le 64. Then ∣nk−Q∣≤Qηk+∣τk∣<2⋅2−55.3+2−56.9<0.82⋅2−54+0.14⋅2−54<2−54|n_k - Q| \le Q\eta_k + |\tau_k| < 2 \cdot 2^{-55.3} + 2^{-56.9} < 0.82 \cdot 2^{-54} + 0.14 \cdot 2^{-54} < 2^{-54}. ■\blacksquare

Proof of the theorem. Let q∗q^{*} be the integer nearest to Q/vQ/v. By the no-midpoint property of quotients (a binary quotient A/BA/B of pp-bit significands is never the midpoint of two consecutive pp-bit numbers, see floating-point division), Q/vQ/v is not a half-integer, so q∗q^{*} is unique and ∣Q/v−q∗∣<12|Q/v - q^{*}| < \tfrac12.

Line L7 rounds nk/vn_k/v to the nearest integer qq, so ∣q−nk/v∣≤12|q - n_k/v| \le \tfrac12. By Lemma 3, ∣nk−Q∣/v<2−54/2−53=12|n_k - Q|/v < 2^{-54}/2^{-53} = \tfrac12. Hence ∣q−Q/v∣<1|q - Q/v| < 1, and q∈{q∗−1,q∗,q∗+1}q \in \{q^{*} - 1, q^{*}, q^{*} + 1\}.

Line L8 tests Q<(q−12)vQ < (q - \tfrac12)v, that is 2Ma2s<(2q−1)Mb2 M_a 2^{s} < (2q - 1) M_b with A=Ma2−52A = M_a 2^{-52}, B=Mb2−52B = M_b 2^{-52} and v=2−sv = 2^{-s}, exactly in integers. It holds if and only if q>q∗q > q^{*}, that is q=q∗+1q = q^{*} + 1, and then it decrements qq to q∗q^{*}. Line L9 tests Q>(q+12)vQ > (q + \tfrac12)v, which holds if and only if q<q∗q < q^{*}, and increments qq. After both lines q=q∗q = q^{*}.

Finally q∗v=RN(Q)q^{*} v = \mathrm{RN}(Q): Q∈[1,2)Q \in [1, 2) with v=2−52v = 2^{-52} or Q∈(12,1)Q \in (\tfrac12, 1) with v=2−53v = 2^{-53}, so the binary64 numbers near QQ are exactly the multiples of vv (and q∗vq^{*} v may be the power of two at the top of the binade, which is also a binary64 number). Line L10 multiplies by 2ea−eb2^{e_a - e_b}, which is exact when the result is normal, so it returns RN(Q)2ea−eb=RN(a/b)\mathrm{RN}(Q) 2^{e_a - e_b} = \mathrm{RN}(a/b). Ties never occur, so the result is the same for ties-to-even and ties-away. ■\blacksquare

Remarks. The correctness of the result rests on lines L8 and L9 and on Lemma 3; the iteration itself needs no rounding analysis beyond Lemma 2. With k=2k = 2 the bound of Lemma 3 is only about 2−262^{-26}, the correction can be off by many units, and the tests confirm that the result is then often wrong. Hardware implementations replace the exact integer comparisons by the sign of a remainder a−qba - qb computed with a multiplier, which is the same test.

Back to Goldschmidt division