Introduction
This article is a follow-up to part 1 where multiple-precision addition, subtraction, and multiplication for non-negative integers was discussed. This article deals with division. Again, the theoretic foundation is based on Section 4.3.1, The Classical Algorithms, of The Art of Computer Programming, Volume 2, by Donald E. Knuth.
Fundamentals
With u=(um−1…u1u0)b and v=(vn−1…v1v0)b we wish to compute the integer quotient q=⌊u/v⌋. As a bonus, our algorithm will also produce the remainder r such that u=qv+r with 0≤r<v. We will assume that v=0 (otherwise the result is undefined) and that u≥v (otherwise the result is trivially q=0 and r=u). Since bm−1≤u<bm and bn−1≤v<bn we have bm−n−1<q<bm−n+1, so q can be represented using m−n or m−n+1 digits: q=(qm−n…q1q0)b where qm−n may be zero, but in which case qm−n−1=0 (if m>n).
We will now consider (long) division from a top-level point of view. It is actually just a formalization of the well-known pencil-and-paper method:
Algorithm G. Given u=(um−1…u1u0)b, um−1=0 and v=(vn−1…v1v0)b, vn−1=0, with m≥n>0, this algorithm outlines how to compute the quotient q=(qm−n…q1q0)b=⌊u/v⌋ (we may have qm−n=0 in which case qm−n−1=0 if m>n) and the remainder r such that u=qv+r, 0≤r<v.
- G1. u(m−n+1)←(0um−1…u1u0)b.
- G2. k←m−n.
- G3. qk←⌊(uk+n(k+1)…uk+1(k+1)uk(k+1))b/v⌋.
- G4. Set u(k)←u(k+1)−qkbkv or, equivalently,
(uk+n(k)…uk+1(k)uk(k))b(uk−1(k)…u1(k)u0(k))b←(uk+n(k+1)…uk+1(k+1)uk(k+1))b−qkv,←(uk−1(k+1)…u1(k+1)u0(k+1))b.
- G5. If k=0 then set r←u(0) and exit. Otherwise set k←k−1 and go to step G3.
An essential invariant of this algorithm is
(uk+n−1(k)…uk+1(k)uk(k))b<vfork=0,1,…,m−n+1.
This can be seen as follows. For k=m−n+1 the invariant is ensured by introducing a zero as the most significant digit of u(m−n+1) in step G1. For k=0,1,…,m−n we see from steps G3 and G4 that (uk+n(k)…uk+1(k)uk(k))b=(uk+n(k+1)…uk+1(k+1)uk(k+1))b mod v and the inequality follows.
Note that the invariant implies that uk+n(k)=0 for k=0,1,…,m−n. Furthermore we have that
(uk+n(k+1)…uk+1(k+1)uk(k+1))b=(uk+n(k+1)…uk+1(k+1))b⋅b+uk(k+1)≤(v−1)b+(b−1)=vb−1
from which we see that the quotients qk computed in step G3 are non-negative and smaller than b, as they should be.
Finally, we can verify that the algorithm computes what we intended. We have
r=u(0)=u(1)−q0b0v=u(2)−q1b1v−q0b0v=…=u(m−n+1)−(qm−nbm−n+⋯+q0b0)v=u−qv.
Now for some practical aspects. Note first that all of the u(k) variables can in fact be represented by a single variable and simply overwrite its digits along the way—thus ending up with the remainder. Note also that any of the remainder's digits may be zero.
Finally, how do we compute the quotient in step G3? That is in fact the central part of the division algorithm and is the subject of the rest of this article.
Simple Division
Let us first consider computing the quotient of step G3 in the special case n=1. So let us assume that u<b2, 0<v<b and ⌊u/v⌋<b. As in part 1 we wish to use b=bT for some intrinsic integer (C++) type T
, and just as most CPUs have built-in instructions for the most basic addition, subtraction, and multiplication operations, this is also the case for division. More specifically, obtaining the quotient q and remainder r such that u=qv+r, where 0≤q<b, 0≤r<v, is a quite common instruction. And that instruction is exactly what we need in steps G3 and G4 above.
However, it is not possible to access such an instruction directly through standard C++. As we did for multiplication, we therefore split u and v into smaller parts and do the operation at this smaller scale. So let us assume a number h exists such that h2=b. We now set u=(u3′u2′u1′u0′)h and v=(v1′v0′)h and use the algorithms of this article on this representation. A 'simple division' is now of the type (u3′h+u2′)/v1′ and we can do that directly in C++.
We will not go further into the implementation of 'double-precision division', but an example implementation is the function double_div
of lowlevel_generic.hpp
. You can also see the specialization of this routine for x86 processors with GCC compilers in the file lowlevel_gcc_x86.hpp
.
For n=1 Algorithm G of the previous section can be greatly simplified if we are interested in just the quotient q=(qn−1…q1q0)b and the remainder 0≤r<v:
Algorithm S. Given u=(um−1…u1u0)b and 0<v<b with m≥1 and um−1=0, this algorithm computes the quotient q=(qm−1…q1q0)b=⌊u/v⌋ (we may have qm−1=0 in which case qm−2=0 if m>1) and the remainder r such that u=qv+r, 0≤r<v.
- S1. Set r←0, k←m−1.
- S2. Set qk←⌊(rb+uk)/v⌋, r←(rb+uk) mod v.
- S3. If k=0 then exit. Otherwise set k←k−1 and go to step S2.
Long Division
Let us now consider computing the quotient in step G3 in the case n>1. We therefore assume u=(un…u1u0)b, u<bn+1, and v=(vn−1…v1v0)b, bn−1≤v<bn, with n≥2 and 0≤⌊u/v⌋<b.
We wish to compute q=⌊u/v⌋ as fast as possible. How good is a 'first order' approximation, where we use just the two most-significant digits of u and the most-significant digit of v: (unb+un−1)/vn−1? First of all, if un=vn−1 this quantity equals b and we know that q≤b−1 by assumption, so let us therefore study
q^=min(⌊vn−1unb+un−1⌋,b−1)
This approximate quotient is never too small, as the following theorem states.
Theorem 1. With q^ as defined above we have q≤q^.
Proof. If q^=b−1 then since q≤b−1 by assumption, the statement is true. Assume then that q^=⌊(unb+un−1)/vn−1⌋. From the properties of the floor function we have unb+un−1≤q^vn−1+vn−1−1 and therefore q^vn−1≥unb+un−1−vn−1+1. We then get
u−q^v≤u−q^vn−1bn−1≤unbn+⋯+u0−(unb+un−1−vn−1+1)bn−1=un−2bn−2+⋯+u0−bn−1+vn−1bn−1<vn−1bn−1≤v.
So u−q^v<v and since 0≤u−qv<v we must have q≤q^. ◼
If u and v are scaled appropriately, q^ will never be too large, either.
Theorem 2. With q^ as defined above and vn−1≥⌊b/2⌋, we have q^≤q+2.
Proof. Assume that q^≥q+3. We get
q^≤vn−1unbun−1=vn−1bn−1unbnun−1bn−1≤vn−1bn−1u<v−bn−1u,
since v=vn−1bn−1+⋯+v0≤vn−1bn−1+bn−1. We cannot have v=bn−1 since that would imply q^=q=un. The relation q=⌊u/v⌋ implies q>u/v−1, from which we get
3≤q^−q<v−bn−1u−vu+1=vu(v−bn−1bn−1)+1.
We then have
vu≥2(bn−1v−bn−1)≥2(vn−1−1),
and finally
b−4≥q^−3≥q=⌊u/v⌋≥2(vn−1−1),
which implies vn−1<⌊b/2⌋. ◼
We would expect to come even closer if we consider the 'second order' approximate quotient,
⌊vn−1b+vn−2unb2+un−1b+un−2⌋,
but how much closer? Given some approximate quotient q^, let us compute the corresponding second order residual
unb2+un−1b+un−2−q^(vn−1b+vn−2)=r^b+un−2−q^vn−2,
where r^ is the first order residual,
r^=unb+un−1−q^vn−1.
By studying the sign of the second order residual we can now get closer to the true quotient.
Theorem 3. Let q^ be any approximate quotient and r^ the corresponding first order residual. Now if q^vn−2>br^+un−2 then q<q^.
Proof. Assume q^vn−2>br^+un−2, equivalent to r^b+un−2−q^vn−2+1≤0. We then have
u−q^v≤u−q^vn−1bn−1−q^vn−2bn−2=bn−1(unb+un−1−q^vn−1)+un−2bn−2+⋯+u0−q^vn−2bn−2<bn−1r^+un−2bn−2+bn−2−q^vn−2bn−2=bn−2(r^b+un−2−q^vn−2+1)≤0.
So u−q^v<0≤u−qv which implies q<q^. ◼
Theorem 4. Let q^ be any approximate quotient and r^ the corresponding first order residual. Now if q^vn−2≤br^+un−2 then q^≤q+1.
Proof. Let q^vn−2≤br^+un−2 and assume q^≥q+2. Now since u−qv<v we get
u<(q+1)v≤(q^−1)v<q^(vn−1bn−1+vn−2bn−2+bn−2)−v<q^vn−1bn−1+q^vn−2bn−2+bn−1−v≤q^vn−1bn−1+(br^+un−2)bn−2+bn−1−v=unbn+un−1bn−1+un−2bn−2+bn−1−v≤unbn+un−1bn−1+un−2bn−2≤u.
This claims that u<u, a contradiction, so our assumption q^≥q+2 must have been wrong. ◼
We now have the following procedure for computing q^, a very close estimate to q:
Algorithm Q. Let u=(un…u1u0)b and v=(vn−1…v1v0)b, vn−1=0, with n≥2 and 0≤⌊u/v⌋<b (any digit of u can be zero and note that the only digits accessed are un, un−1, un−2, vn−1, and vn−2). The algorithm computes an integer q^ such that q^−1≤⌊u/v⌋≤q^ (Theorems 1 and 4).
- Q1. Set q^←⌊(unb+un−1)/vn−1⌋ and r^←(unb+un−1) mod vn−1. If q^=b (division overflow when b=bT) set q^←q^−1 and r^←r^+vn−1 (dealing with division overflow can be avoided by setting q^←b−1 and r^←un+un−1 if vn−1=un).
- Q2. While r^<b and q^vn−2>br^+un−2, set q^←q^−1 and r^←r^+vn−1 (Theorem 2 assures that this while-loop is executed at most two times if vn−1≥⌊b/2⌋. The check r^<b is not necessary but makes sure that we don't deal with numbers that are b2 or larger in the subsequent comparison).
We can now combine Algorithm G of the Fundamentals section with the just obtained knowledge of approximating the quotient in the following algorithm for long division:
Algorithm L. Given u=(um−1…u1u0)b, um−1=0 and v=(vn−1…v1v0)b, vn−1=0, with m≥n>1, this algorithm computes the quotient q=(qm−n…q1q0)b=⌊u/v⌋ (we may have qm−n=0 in which case qm−n−1=0 if m>n) and the remainder r such that u=qv+r, 0≤r<v.
- L1. Set v←d⋅v such that vn−1≥⌊b/2⌋ (letting d be a power of two is usually the best choice). Similarly, set (um…u1u0)b←d⋅u (ensure u gets n+1 digits, setting um=0 if necessary).
- L2. Set k←m−n.
- L3. Find q^ such that q^−1≤⌊(uk+n…uk+1uk)b/v⌋≤q^ (use Algorithm Q described above).
- L4. Make the update (uk+n…uk+1uk)b←(uk+n…uk+1uk)b−q^v.
- L5. If the subtraction of step L4 produces a borrow (the result is negative) do q^←q^−1 and (uk+n…uk+1uk)b←(uk+n…uk+1uk)b+v.
- L6. Set qk=q^.
- L7. If k=0 set r←u/d and exit. Otherwise set k←k−1 and go to step L3.
The normalization in step L1 such that vn−1≥⌊b/2⌋ does two things. Firstly, it makes sure that the while-loop of the q^-computation executes at most two times. Secondly, the probability that the adding back in step L5 must be executed is of order 2/b (a proof can be found in Knuth's book).
Concluding Remarks
This and the previous article have now covered addition, subtraction, multiplication, and division of non-negative integers.
Update 2010-07-03: See the project page for more information.