taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Exercise · Volume 1 · 1.2.2-25

Exercise 1.2.2-25

not stated yet in Lean solution written

Rating22
KindK1, prove: show that a stated fact is true from the written solution
SectionSection 1.2.2 of Volume 1. Companion chapter: Numbers, powers and logarithms

The task

Show that the shift-and-subtract algorithm terminates and that it computes y≈log⁡bxy \approx \log_b x for 1≤x<21 \le x < 2.

The exact wording is in your copy of the book.

Hints

Open one hint at a time. Try the exercise again after each.

Hint 1

Find what is invariant in steps L3 and L4 about zz and xx. It is a relation between zz, xx and 2−k2^{-k}.

Hint 2

Find the quantity that does not change when xx is replaced by x−zx - z and yy grows by a table entry.

Hint 3

For the end, count how often step L4 can run: how large is the decrease in xx each time, and how small can x−1x - 1 be if x≠1x \ne 1?

Solution

Show the solution

Setting. Numbers have pp bits after the binary point, and a right shift cuts the bits that fall off. Write ck=log⁡b(2k/(2k−1))c_k = \log_b(2^k/(2^k - 1)) for the table entries. The input is the number x0x_0 with 1≤x0<21 \le x_0 < 2, and xx is the variable of the algorithm.

(1) The value of zz. Claim: at L2, L3 and L4 the variable zz equals ⌊x2−k⌋p\lfloor x 2^{-k} \rfloor_p, the number x2−kx2^{-k} with the bits after place pp cut. This holds after L1 for k=1k = 1. A shift of z=⌊w⌋pz = \lfloor w \rfloor_p right by one place gives ⌊w/2⌋p\lfloor w/2 \rfloor_p, because ⌊⌊2pw⌋/2⌋=⌊2pw/2⌋\lfloor \lfloor 2^p w \rfloor / 2 \rfloor = \lfloor 2^p w / 2 \rfloor for every real w≥0w \ge 0. So step L3, which halves zz and adds 11 to kk, keeps the claim. Step L4 sets zz from the new xx, so the claim holds after it too. Write x2−k−z=θx 2^{-k} - z = \theta with 0≤θ<2−p0 \le \theta < 2^{-p}.

(2) The reduction. Step L4 runs only when L3 found x−z≥1x - z \ge 1, and it replaces xx by x−z=x(1−2−k)+θx - z = x(1 - 2^{-k}) + \theta. So after L4, x≥1x \ge 1, and 1≤x1 \le x holds at each pass through L2. Without the cut (θ=0\theta = 0) the new value is exactly x(1−2−k)x(1 - 2^{-k}), so log⁡bx\log_b x drops by log⁡b(1/(1−2−k))=ck\log_b(1/(1 - 2^{-k})) = c_k while L4 adds ckc_k to yy. Hence y+log⁡bxy + \log_b x stays constant, and it equals log⁡bx0\log_b x_0 at the end if the cuts are ignored.

(3) Termination. Claim: if x>1x > 1 then step L3 stops with k≤pk \le p, and the value zz is at least 2−p2^{-p}. The variable xx is on the grid of multiples of 2−p2^{-p}, so x>1x > 1 gives x≥1+2−px \ge 1 + 2^{-p}. Also x<2x < 2 always, since xx only decreases. For k=pk = p, z=⌊x2−p⌋p=2−pz = \lfloor x 2^{-p} \rfloor_p = 2^{-p}, so x−z≥1x - z \ge 1 and the test of L3 fails. Thus L3 stops at some k≤pk \le p. For k≤pk \le p and x≥1x \ge 1, x2−k≥2−k≥2−px2^{-k} \ge 2^{-k} \ge 2^{-p}, so z≥2−pz \ge 2^{-p}. Now each execution of L4 lowers xx by at least 2−p2^{-p}, and x≥1x \ge 1 stays true. The number of executions is finite, and the only way out is L2 with x=1x = 1. So the algorithm terminates, and at the end x=1x = 1.

(4) Value of the result. At the end yy is the sum of the table entries used. Without cuts, log⁡bx0=y\log_b x_0 = y exactly. With the cuts, the new xx is a little larger than x(1−2−k)x(1 - 2^{-k}), so each L4 adds a small error uj≥0u_j \ge 0 to the sum y+log⁡bxy + \log_b x, and at the end y=log⁡bx0+∑ujy = \log_b x_0 + \sum u_j. The sum of these errors is bounded in Exercise 1.2.2-26, and it goes to 00 as pp grows. So yy approximates log⁡bx0\log_b x_0. □\square

Check

Take b=2b = 2, p=8p = 8 and x0=1.5x_0 = 1.5. A python run with 88 bits and exact table values ends with x=1x = 1 after 55 executions of L4. It gives y=0.58799…y = 0.58799\ldots against the true value lg⁡1.5=0.58496…\lg 1.5 = 0.58496\ldots. The difference 0.00300.0030 lies between 00 and the bound 5⋅2−7/ln⁡2=0.0565 \cdot 2^{-7}/\ln 2 = 0.056 of Exercise 1.2.2-26.

Remarks

This is the method of Briggs for tables of logarithms. It uses the same idea as a division by repeated subtraction of shifted values: the quotient is not formed, only a running sum of constants. Exercise 1.2.2-28 gives the matching method for powers.

This solution is checked, and a person has not reviewed it yet. The source.

In Lean

not stated yet The exercise is not stated in Lean yet.

The status is computed from the Lean build. The node of the map.

Something is wrong? Report it.