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-28

Exercise 1.2.2-28

not stated yet in Lean solution written

RatingM30
MarkersM (mathematical)
KindK5, design an algorithm: give a method for a task, often with a cost bound from the written solution
SectionSection 1.2.2 of Volume 1. Companion chapter: Numbers, powers and logarithms

The task

Give a method to compute bxb^x for 0≤x<10 \le x < 1 by shifts, additions and subtractions, and bound its error.

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

Turn Exercise 1.2.2-25 around: keep a table of ek=log⁡b(1+2−k)e_k = \log_b(1 + 2^{-k}) and build bxb^x as a product of factors 1+2−k1 + 2^{-k}.

Hint 2

A multiplication by 1+2−k1 + 2^{-k} is y+(y shifted right k)y + (y \text{ shifted right } k).

Hint 3

For the accuracy, compare the rounded table and the cut shifts with the exact values, as in Exercise 1.2.2-26.

Solution

Show the solution

Let b>1b > 1, let pp be the number of bits after the point, and let the table hold ek=log⁡b(1+2−k)e_k = \log_b(1 + 2^{-k}) for k=1,…,pk = 1, \ldots, p, rounded to ek′e'_k with ∣ek′−ek∣≤η|e'_k - e_k| \le \eta.

Method.

  • P1. Set r←xr \leftarrow x, y←1y \leftarrow 1, k←1k \leftarrow 1.
  • P2. If k>pk > p, stop, and yy is the result.
  • P3. If r<ek′r < e'_k, set k←k+1k \leftarrow k + 1 and go to P2.
  • P4. Set r←r−ek′r \leftarrow r - e'_k, y←y+(y shifted right k)y \leftarrow y + (y \text{ shifted right } k) and go to P2.

Why it works. Each execution of P4 keeps y⋅br≈bxy \cdot b^{r} \approx b^x, since br+ek′≈br(1+2−k)b^{r + e'_k} \approx b^{r}(1 + 2^{-k}). The index kk goes up only when r<ek′r < e'_k, so at the end 0≤r<ep′0 \le r < e'_p. Therefore rr is tiny: ep′≤2−p/ln⁡b+ηe'_p \le 2^{-p}/\ln b + \eta, and br≈1b^r \approx 1.

Accuracy. Let NN be the number of executions of P4 and let PP be the product of the factors 1+2−kj1 + 2^{-k_j} used. The computed yy has been cut in each shift, so each step gives ynew=y(1+2−k)−θy_{\text{new}} = y(1 + 2^{-k}) - \theta with 0≤θ<2−p0 \le \theta < 2^{-p}. Here y≥1y \ge 1, so the cut costs at most the relative amount 2−p2^{-p}, and the later factors are at most PP in total. Hence P(1−N2−p)≤y≤PP(1 - N 2^{-p}) \le y \le P.

The exact subtractions give x=r+∑ekj′x = r + \sum e'_{k_j}. With D=∑(ekj′−ekj)D = \sum (e'_{k_j} - e_{k_j}) and ∣D∣≤Nη|D| \le N\eta, b∑e′=PbDb^{\sum e'} = P b^{D}, so bx=brPbDb^x = b^{r} P b^{D}, and ybx=yP b−r−D.\frac{y}{b^x} = \frac{y}{P} \, b^{-r - D}. Since 0≤r<ep′0 \le r < e'_p, br<(1+2−p) bηb^{r} < (1 + 2^{-p})\,b^{\eta}. So 1−N2−p1+2−p b−(N+1)η≤ybx≤bNη.\frac{1 - N2^{-p}}{1 + 2^{-p}} \, b^{-(N+1)\eta} \le \frac{y}{b^x} \le b^{N\eta}. For an exact table the number of executions is N<p−1+ln⁡b/ln⁡1.5N < p - 1 + \ln b / \ln 1.5. For k≥2k \ge 2 the step P4 runs at most once for each kk, because when P2 first meets kk, r<ek−1=log⁡b(1+21−k)r < e_{k-1} = \log_b(1 + 2^{1-k}), and twice eke_k is more than that, since (1+2−k)2>1+21−k(1 + 2^{-k})^2 > 1 + 2^{1-k}. For k=1k = 1 it runs less than ln⁡b/ln⁡1.5\ln b / \ln 1.5 times, as r<1r < 1.

Check

Take b=2b = 2, p=20p = 20, x=0.5x = 0.5 and an exact table. The method gives y=1.4142094y = 1.4142094 against 2=1.4142136\sqrt 2 = 1.4142136, a relative error of 3.0⋅10−63.0 \cdot 10^{-6}, within the bound N2−p≈9⋅10−6N 2^{-p} \approx 9 \cdot 10^{-6} for N=9N = 9.

Remarks

R. Feynman is credited with this method. The final factor br≈1+rln⁡bb^r \approx 1 + r\ln b can be added by one more multiplication if the last bits matter. The book's answer takes another route, with the complementary variable 1−x1 - x and factors 1−2−k1 - 2^{-k}.

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.