Notebook for 2023-03-20
As a running example in this lecture, we looked at numerical computations for the reciprocal function computing $r = 1/d$ (assuming that we didn't have division already readily at hand).
Long division
This is binary long division (the binary version of the algorithm you learned in school). It gets about one bit of additional accuracy per step.
reciprocal_div (generic function with 1 method)
Bisection
Bisection uses the fact that the reciprocal $1/d$ likes between a lower bound $l$ such that $dl-1 < 0$ and an upper bound $h$ such that $dh-1 > 0$. Bisection starts with an initial interval $[l,h]$ and repeatedly replaces it with a smaller interval $[l,m]$ or $[m,h]$ (for $m = (l+h)/2$) such that there continues to be a sign change of the function $dx-1$ between the two endpoints.
reciprocal_bisect (generic function with 1 method)
A modified Newton approach
This is the fixed point iteration
$$x_{k+1} = x_k-\frac{dx_k-1}{\hat{d}}$$
where $\hat{d}$ is a power of 2 as close as possible to $d$. If we divided by $d$ instead of $\hat{d}$, we would converge in one step –- but we are doing all this under the assumption that we don't know how to divide in general. On the other hand, division by $\hat{d}$ is easy in binary floating point when $\hat{d}$ is a power of two.
reciprocal_newtonish (generic function with 1 method)
Newton iteration
Let $g(x) = x^{-1} - d$. Newton iteration for this function gives (with some algebra)
$$x_{k+1} = x_k-\frac{g(x_k)}{g'(x_k)} = x_k (2-d x_k)$$
which we can compute with only a subtraction and two multiplications. The iteration converges quadratically from good enough initial guesses; that means that the error at step $k+1$ is proportional to the square of the error at step $k$. On a log scale, the error curve looks like a downward-facing parabola.
reciprocal_newton (generic function with 1 method)
Comparing methods
Let's wrap up by looking at all four methods we've considered on a common axis. It will take something like 53 iterations for bisection or long division to reach full accuracy; our modified Newton takes something like 25 steps; and our Newton iteration reaches correct rounding in only 6 steps.