How Do Calculators Calculate? Helmut Knaust Department of Mathematical Sciences University of Texas at El Paso Helmut Knaust How Do Calculators Calculate? April 25, 1997 1 / 18
History We give an introduction to the CORDIC method used my most handheld calculators (such as the ones by Texas Instruments and Hewlett-Packard) to approximate the standard transcendental functions. The CORDIC algorithm does not use Calculus based methods such as polynomial or rational function approximation. Helmut Knaust How Do Calculators Calculate? April 25, 1997 2 / 18
History The CORDIC (= COordinate Rotation DIgital Computer) algorithm was developed by Jack E. Volder in 1959. His objective was to build a real-time navigational computer for use on aircrafts, so he was primarily interested in computing trigonometric functions. Subsequently, the CORDIC scheme was extended by J. Walther in 1971 to other transcendental functions. Helmut Knaust How Do Calculators Calculate? April 25, 1997 3 / 18
Introduction Hand-held calculators do not convert numbers to base 2. They use a binary-coded decimal (BCD) system instead. Calculators can only perform four operations inexpensively: 1 Addition and Subtraction 2 Storing in memory and Retrieving from memory 3 Digit shift (multiplication/division by the base) 4 Comparisons Helmut Knaust How Do Calculators Calculate? April 25, 1997 4 / 18
Introduction The CORDIC Algorithm is a unified computational scheme to perform 1 multiplication and division 2 computations of the trigonometric functions sin, cos, arctan 3 computations of the hyperbolic trigonometric functions sinh, cosh, arctanh 4 and consequently can also compute the exponential function, the natural logarithm and the square root Helmut Knaust How Do Calculators Calculate? April 25, 1997 5 / 18
Introduction The CORDIC Algorithm (Binary Version) x k+1 = x k mδ k y k 2 k (1) y k+1 = y k + δ k x k 2 k (2) z k+1 = z k δ k ε k (3) δ k = ±1 (depending on y k or z k ) (4) The ε k s are prestored constants. m = 0, ±1 (5) Helmut Knaust How Do Calculators Calculate? April 25, 1997 6 / 18
Example 1 A Warm-Up Example: Multiplication To compute the product of a and b for a 2 and b 2 we let The scheme becomes m = 0, ε k = 2 k. x k+1 = x k (= x 0 ) (6) y k+1 = y k + δ k x k 2 k (7) z k+1 = z k δ k 2 k (8) δ k = sgn(z k ) (9) x 0 = a (10) y 0 = 0 (11) Helmut Knaust How Do Calculators Calculate? April 25, 1997 7 / 18
Example 1 From the previous examples we suspect that z n 0 and that y n ab. Let s check: Theorem z n+1 2 n Proof by induction: By the restriction placed on b z 0 = b 2 ( 1) = 2. Now suppose z n 2 (n 1) = 2 n+1. By definition z n+1 = z n sgn(z n )2 n. There are two cases to consider: If 0 z n 2 n+1, then sgn(z n ) = 1, and consequently 2 n z n+1 2 n. If on the other hand 2 n+1 z n < 0, then sgn(z n ) = 1, and consequently 2 n z n+1 2 n. Helmut Knaust How Do Calculators Calculate? April 25, 1997 8 / 18
Example 1 Theorem y n+1 ab 2 n+1 Proof: Note that the iterative formulas imply: n z n+1 = z 0 δ k 2 k, k=0 and Consequently y n+1 = y 0 + ( n ) δ k 2 k x 0. k=0 y n+1 ab = y n+1 x 0 z 0 = (y 0 + (z 0 z n+1 )x 0 ) x 0 z 0 = 0 z n+1 x 0 2 n 2 = 2 n+1 Helmut Knaust How Do Calculators Calculate? April 25, 1997 9 / 18
Example 2 An Example: Computing sin and cos To compute sin θ and cos θ for θ π/2 we let m = 1, ε k = arctan(2 k ). We also define n C = cos(ε k ). k=0 The scheme becomes x k+1 = x k δ k y k 2 k (13) y k+1 = y k + δ k x k 2 k (14) z k+1 = z k δ k ε k (15) δ k = sgn(z k ) (16) x 0 = C (17) y 0 = 0 (18) z 0 = θ (19) Helmut Knaust How Do Calculators Calculate? April 25, 1997 10 / 18
The CORDIC Theorem Theorem The Cordic Representation Theorem Suppose (ε k ) n k=0 is a decreasing sequence of positive real numbers satisfying n ε k + ε n j=k+1 for k = 0, 1,..., n, and suppose r is a real number such that n r ε j. ε j k=0 If s 0 = 0, and s k+1 = s k + δ k ε k, for k = 0, 1,..., n, where δ k = sgn (r s k ). Then n r s n+1 = r δ k ε k < ε n. Proof: By induction. k=0 Helmut Knaust How Do Calculators Calculate? April 25, 1997 11 / 18
The CORDIC Theorem The previous theorem and some computations tell us that it is possible to write the angle r = θ as a combination of the angles ε k, more precisely, we can choose δ k = ±1 so that Set s n+1 = n k=0 δ kε k. θ n δ k ε k < ε n. k=0 Helmut Knaust How Do Calculators Calculate? April 25, 1997 12 / 18
The CORDIC Theorem 0.6 1 0.5 3 6 5 0.4 4 0.3 2 0.2 0.1 Θ 0 0.2 0.4 0.6 0.8 1.0 x Helmut Knaust How Do Calculators Calculate? April 25, 1997 13 / 18
The CORDIC Theorem We then have the following Theorem If x 0 = n k=0 cos ε k and y 0 = 0, then x n+1 = cos s n+1 and y n+1 = sin s n+1. Proof (by induction, sketch): x n+1 = x n δ n y n 2 n = (cos s n cos ε n ) δ n (sin s n cos ε n ) tan ε n = cos s n cos ε n δ n sin s n sin ε n = cos(s n + δ n ε n ) = cos(s n+1 ) Helmut Knaust How Do Calculators Calculate? April 25, 1997 14 / 18
Computing Transcendental Functions m = 1 1 x n+1 cos θ, y n+1 sin θ: Trigonometric Functions ε k = arctan(2 k ), δ k = sgn z k, x 0 = n k=0 cos(ε k), y 0 = 0, z 0 = θ 2 z n+1 arctan(y 0 /x 0 ): ε k = arctan(2 k ), δ k = sgn y k, z 0 = 0 Helmut Knaust How Do Calculators Calculate? April 25, 1997 15 / 18
Computing Transcendental Functions m = 1 1 x n+1 cosh θ, y n+1 sinh θ, and thus x n+1 + y n+1 e θ : Hyperbolic Functions ε k = arctanh(2 k ) (repeated), δ k = sgn z k, x 0 = C, y 0 = 0, z 0 = θ 2 z n+1 arctanh(y 0 /x 0 ), x n+1 C x 2 0 y 2 0, and thus we obtain an approximation of ln x = 2 arctanh ε k = arctanh(2 k ) (repeated), δ k = sgn y k, z 0 = 0 ( ) x 1 : x + 1 Helmut Knaust How Do Calculators Calculate? April 25, 1997 16 / 18
Multiplication and Division Multiplication and Division m = 0 1 y n+1 x 0 z 0 : ε k = 2 k, δ k = sgn z k, y 0 = 0 2 z n+1 y 0 /x 0 : ε k = 2 k, δ k = sgn y k, z 0 = 0 Helmut Knaust How Do Calculators Calculate? April 25, 1997 17 / 18
References 1 Charles W. Schelin Calculator Function Approximation American Math. Monthly 90, 1983, 317 325. 2 Jack E. Volder The CORDIC Trigonometric Computing Technique IRE Transcactions EC-8, 1959, 330 334 3 Richard J. Pulskamp and James A. Delaney Computer and Calculator Computation of Elementary Functions UMAP Module 708, 1991. Beamer version: December 2012. Helmut Knaust How Do Calculators Calculate? April 25, 1997 18 / 18