Computing Transcendental Functions - LSU Math

33
Computing Transcendental Functions Iris Oved [email protected] 29 April 1999 Exponential, logarithmic, and trigonometric functions are transcendental. They are peculiar in that their values cannot be computed in terms of finite compositions of arithmetic and/or algebraic operations. So how might one go about computing such functions? There are several approaches to this problem, three of which will be outlined below. We will begin with Taylor approxima- tions, then take a look at a method involving Minimax Polynomials, and we will conclude with a brief consideration of CORDIC approximations. While the discussion will specifically cover methods for calculating the sine function, the procedures can be generalized for the calculation of other transcendental functions as well. 1 Taylor Approximations One method for computing the sine function is to find a polynomial that allows us to approximate the sine locally. A polynomial is a good approximation to the sine function near some point, x = a, if the derivatives of the polynomial at the point are equal to the derivatives of the sine curve at that point. The more derivatives the polynomial has in common with the sine function at the point, the better the approximation. Thus the higher the degree of the polynomial, the better the approximation. A polynomial that is found by this method is called a Taylor Polynomial. Suppose we are looking for the Taylor Polynomial of degree 1 approximating the sine function near x = 0. We will call the polynomial p(x) and the sine function f (x). We want to find a linear function p(x) such that p(0) = f (0) and p 0 (0) = f 0 (0). Since we are looking for a polynomial of degree 1, we know that it will be of the form p(x)= Bx + A. Now we set p(0) = f (0). Since we know that f (0) = 0, we have p(0) = 0. 1

Transcript of Computing Transcendental Functions - LSU Math

Page 1: Computing Transcendental Functions - LSU Math

Computing Transcendental Functions

Iris Oved [email protected]

29 April 1999

Exponential, logarithmic, and trigonometric functions are transcendental.They are peculiar in that their values cannot be computed in terms of finitecompositions of arithmetic and/or algebraic operations. So how might one goabout computing such functions? There are several approaches to this problem,three of which will be outlined below. We will begin with Taylor approxima-tions, then take a look at a method involving Minimax Polynomials, and wewill conclude with a brief consideration of CORDIC approximations. Whilethe discussion will specifically cover methods for calculating the sine function,the procedures can be generalized for the calculation of other transcendentalfunctions as well.

1 Taylor Approximations

One method for computing the sine function is to find a polynomial that allowsus to approximate the sine locally. A polynomial is a good approximation tothe sine function near some point, x = a, if the derivatives of the polynomial atthe point are equal to the derivatives of the sine curve at that point. The morederivatives the polynomial has in common with the sine function at the point,the better the approximation. Thus the higher the degree of the polynomial,the better the approximation. A polynomial that is found by this method iscalled a Taylor Polynomial.

Suppose we are looking for the Taylor Polynomial of degree 1 approximatingthe sine function near x = 0. We will call the polynomial p(x) and the sinefunction f(x). We want to find a linear function p(x) such that p(0) = f(0) andp′(0) = f ′(0).

Since we are looking for a polynomial of degree 1, we know that it will be ofthe form

p(x) = Bx+A.

Now we setp(0) = f(0).

Since we know that f(0) = 0, we have

p(0) = 0.

1

Page 2: Computing Transcendental Functions - LSU Math

Substituting x = 0 into p(x) gives

p(0) = 0B +A.

Thus, in our polynomial, we know the coefficient A = 0.To find the value of the coefficient B, we take the derivative of our linear

function and we have:p′(x) = B.

Now we setp′(0) = f ′(0).

Since we know that the derivative of the sine function is the cosine function, weknow that f ′(0) = cos(0) = 1. So we have

p′(0) = 1.

Substituting x = 0 into p′(x), we get

p′(0) = B.

Thus we have the coefficient B = 1.Now that we have both coefficients, we arrive at the Taylor Polynomial of

degree 1 that approximates the sine curve near x = 0:

p(x) = x.

Notice that this is the line tangent to the sine function at x = 0.1

1Indeed, the degree 1 Taylor Polynomial of any function is necessarily the line tangent tothe function at the point about which the above procedures are performed.

2

Page 3: Computing Transcendental Functions - LSU Math

-6 -4 -2 2 4 6

-4

-2

2

43

Page 4: Computing Transcendental Functions - LSU Math

Figure 1.1. Sine curve and degree 1 Taylor with −2π ≤ x ≤ 2π

We can see in Figure 1.1 that the degree 1 Taylor Polynomial is a goodapproximation to the sine curve near x = 0, but clearly it gets worse as wemove farther away from that point. Higher degree polynomials have a highernumber of meaningful derivatives (derivatives not equal to zero). Higher degreeTaylor Polynomials thus have more derivatives in common with the functionthat is being approximated. This means that the higher the degree of theTaylor Polynomial, the more accurately it approximates the sine function. Todemonstrate this, let us find the Taylor Polynomial of degree 3, for the sinecurve, centered at x = 0.

Since we are looking for a polynomial of degree 3, we know that it will be ofthe form

p(x) = Dx3 + Cx2 +Bx+A.

As before, we setp(0) = f(0),

and conclude that in our polynomial we have the coefficient A = 0.Next, to find the coefficient B, we take the first derivative of our polynomial

and getp′(x) = 3Dx2 + 2Cx+B.

Now we setp′(0) = f ′(0),

and we have the coefficient B = 1.Now, to find the coefficient C, we take the second derivative of our polyno-

mial and getp′′(x) = 6Dx+ 2C.

Next we setp′′(0) = f ′′(0).

Since we know that the derivative of the cosine function is the negative of thesine function, we know that f ′′(0) = − sin(0) = 0. So we have

p′′(0) = 0.

Substituting x = 0 into p′′(x), we get

p′′(0) = 2C.

Thus we have the coefficient C = 0.Finally, to find the coefficient D, we take the third derivative of our polyno-

mial and getp′′′(x) = 6D.

Now we setp′′′(0) = f ′′′(0).

4

Page 5: Computing Transcendental Functions - LSU Math

Since we know that the derivative of the negative of the sine function is thenegative of the cosine function, we know that f ′′′(0) = − cos(0) = −1. So wehave

p′′′(0) = −1.

Substituting x = 0 into p′′′(x), we get

p′′′(0) = 6D.

Thus we have the coefficient D = −1/6.By this method, we have discovered that the degree 3 Taylor Polynomial

that approximates the sine function near x = 0 is

p(x) = − 16x

3 + 0x2 + x+ 0.

5

Page 6: Computing Transcendental Functions - LSU Math

-6 -4 -2 2 4 6

-4

-2

2

46

Page 7: Computing Transcendental Functions - LSU Math

Figure 1.2. Sine curve and degree 3 Taylor with −2π ≤ x ≤ 2π

Although the degree 3 Taylor Polynomial is a better approximation than thedegree 1 Taylor Polynomial, it still becomes very inaccurate as we move fartheraway from x = 0.

The general formula for the Taylor Polynomial of degree n approximatingsome function f(x) for x near 0 is

f(0) + f ′(0)x+f ′′(0)

2!x2 +

f ′′′(0)

3!x3 + · · · +

f (n)(0)

n!xn.

So suppose we want to find the Taylor polynomial of degree 9 that approxi-mates the sine function near x = 0.2 Substituting f(x) = sin(x) and n = 9 intothe general formula gives

x− x3

3!+x5

5!− x7

7!+x9

9!.

2Since sin(−x) = − sin(x), there are no interesting even-degree Taylor Polynomials atx = 0.

7

Page 8: Computing Transcendental Functions - LSU Math

-6 -4 -2 2 4 6

-4

-2

2

48

Page 9: Computing Transcendental Functions - LSU Math

Figure 1.3. Sine curve and degree 9 Taylor Polynomial with −2π ≤ x ≤ 2π

Figure 1.3 shows that the Taylor Polynomial of degree 9 is a better approxi-mation to the sine curve than the linear and cubic polynomials. Still, even thishigh degree polynomial provides approximations that become worse as we movefarther away from x = 0. We can arrive at better and better approximationsto the sine curve if we find Taylor Polynomials of higher and higher degree.However, no matter how large the degree of the polynomial, since the sine curveis periodic, while polynomials are not, there will always be a point at which thegraph of the polynomial will deviate significantly from the sine curve in somedirection. This is to be expected because the sine function is transcendental.No finite composition of arithmetic and/or algebraic operations will capture thebehavior of a transcendental function.

One solution to this problem is to limit the sine curve to a domain of x-values from which we can infer the sine of any real number. Since the sinefunction has a period of 2π, it is clear that we can infer the sine of any realnumber if we can calculate the sine of any real x in the domain 0 ≤ x ≤ 2π.In this domain, the sine function has a finite number of turns, so a high degreepolynomial would be an adequate approximation to the curve for all real xin the domain. However, we would benefit by further reducing the domain ofinputs so that each of the inputs will be even closer to x = 0. This is importantbecause Taylor Polynomials are good approximations near a point, but not sogood farther away from that point.

9

Page 10: Computing Transcendental Functions - LSU Math

1 2 3 4 5 6

-1

-0.5

0.5

110

Page 11: Computing Transcendental Functions - LSU Math

Figure 1.4. Sine curve with 0 ≤ x ≤ 2π

If we look at the sine curve in the domain 0 ≤ x ≤ 2π, we can see thatthe domain can be further limited. Notice that all of the sine-values at inputsbetween π and 2π are negations of values between 0 and π. As long as we makea note to negate our solution, we can infer the sine of all of the x-values in thedomain π ≤ x ≤ 2π, from the sine of the x-values in the domain 0 ≤ x ≤ π.A polynomial of high degree that resembles the sine curve in this domain willprovide information from which we can infer a reasonable appproximation to thesine of any real x. Still, there is some symmetry in the sine curve that we canexploit to shrink the domain further. The sine of the x-values between π

2 andπ can be inferred from the sine of the x-values between 0 and π

2 by reflectingacross the line x = π

2 . Now the Taylor Polynomial only needs to be a goodapproximation to the sine curve over the limited domain 0 ≤ x ≤ π

2 in order forus to compute the sine function for any real input.

11

Page 12: Computing Transcendental Functions - LSU Math

0.25 0.5 0.75 1 1.25 1.5

0.2

0.4

0.6

0.8

112

Page 13: Computing Transcendental Functions - LSU Math

Figure 1.5. Sine curve with 0 ≤ x ≤ π2

Of course, one can always find further tricks to solving such problems—tricksusually involve further complexities. Perhaps for some practical reasons, accu-racy carries more weight than complexity. In these cases, it may be reasonableto find several Taylor Polynomials that are good approximations near differentpoints, and split the curve accordingly. For example, one might want to finda Taylor Polynomial for the sine curve at x = 0 to approximate the sine ofvalues in the first part of the domain, and another Taylor Polynomial for thesine curve at x = π

2 to approximate the sine of values in the second part ofthe domain. Suppose we want to use the degree 5 Taylor Polynomial at x = 0and the degree 4 Taylor Polynomial at x = π

2 .3 A reasonable place to dividethe domain is the point at which these two polynomials intersect. Setting thedegree 5 Taylor Polynomial at x = 0 equal to the degree 4 Taylor Polynomialat x = π

2 will allow us to find the x-value at which these polynomials intersect.Entering these polynomials into a graphing calculator and zooming in, we canguess that the point of intersection is approximately at x = 0.92.

Below is an implementation of the above method as a TI-82 calculator pro-gram. 4 The polynomials are evaluated by Horner’s method, which is to writea polynomial of the form ax4 + bx3 + cx2 + dx + e in the form

[((ax + b)x +

c)x+ d

]x+ e. Rewriting polynomials in this way increases the efficiency of the

computation by reducing the number of operations. Rather than computingfourth degree polynomials using 10 multiplications, Horner’s method gives thesame result with only 4 multiplications (the number of additions is unchanged).

:Prompt X

:If (X<0):pi-X->X //limit to positive

:2pi(fPart(X/(2pi)))->X //limit to 0<=x<=2pi

:1->N //initialize negation flag

:If(X>pi) :Then //limit to 0<=x<=pi

:X-pi->X

:-N->N //flip negation flag

:End

:If(X>pi/2):pi-X->X //limit to 0<=x<=pi/2

:If(X<=0.92)

:((((((1/120)X)X+(-1/6))X)X+1)X)->G //degree 5 Taylor at x=0

:If(X>0.92) :Then

:(X-pi/2)^2->X //degree 4 Taylor at x=Pi/2

:(X(X/24-0.5))+1->G

:End

:Disp N*G

3Since sin(π2− x) = sin(x), there are no interesting odd-degree Taylor Polynomials at

x = π2

.4Note: When entering the program, use the π key in place of “pi”, and use the “STO→”

key in place of “->”.

13

Page 14: Computing Transcendental Functions - LSU Math

2 Minimax Polynomials

Taylor Polynomials become less accurate as we get farther away from the pointat which they are centered. This means that, if we have a Taylor Polynomial forthe sine curve centered at x = 0 over some interval around x = 0, we expect thatthe maximum errors between the polynomial and the sine function will occurat the endpoints of the interval. If we have the degree 5 Taylor Polynomial atx = 0, the error function is

err(x) = sin(x) −[

1120x

5 − 16x

3 + x

].

Since we can infer the sine of any real x from the sine of x-values between 0and π

2 , the farthest point from x = 0 that we need to consider is at x = π2 .

Comparing the the graphs of sin(x) and 1120x

5 − 16x

3 + x, we see that themaximum distance between them, on the domain 0 ≤ x ≤ π

2 , indeed occurs atπ2 . Substituting x = π

2 into err(x) gives

err(π2 ) = 0.004525.

Thus the maximum error (in absolute value) of the degree 5 Taylor approxima-tion to the sine curve centered at x = 0 is 0.004525.

Minimax Polynomials are polynomials that minimize the maximum error.Rather than being highly accurate near a specific point and increasingly in-accurate away from that point, these approximations are reasonaby accurateover an entire domain. According to Chebyshev’s Equioscillation Theorem,5

for every continuous function over the interval a ≤ x ≤ b, there exist Mini-max Polynomials of every degree, and the Minimax Polynomial of degree ≤ nis uniquely characterized by the property that it has degree n and there existat least n + 2 points in the domain a ≤ x ≤ b such that, at these points, theerrors between the polynomial and the function oscillate in sign and are equalin magnitude. Symbolically, if m(x) is the degree n Minimax Polynomial ap-proximating a function f(x) on the interval a ≤ x ≤ b, then there exist n + 2points, a ≤ x1 < x2 < · · · < xn+2 ≤ b, such that:6

f(x1) −m(x1) = m(x2) − f(x2) = f(x3) −m(x3) = . . . .

Like Taylor Polynomials, Minimax Polynomials of higher degree are better ap-proximations than ones of lower degree.

2.1 Linear Minimax Polynomials

Suppose we want to find the degree 1 Minimax Polynomial for the sine curveover the domain 0 ≤ x ≤ π

2 . This means that we are looking for a polynomial

5I don’t have a good reference for this theorem. My teacher, Alexander Perlis, learnedabout it from a fellow graduate student, Dan Coombs, who learned about it in a class severalyears ago.

6Notice the switching in the order of subtraction.

14

Page 15: Computing Transcendental Functions - LSU Math

of the form p(x) = mx + b such that the maximum error between the polyno-mial and the sine curve is minimized. This would be the optimal straight lineapproximation to the sine curve over that domain.

The error between the sine curve and the polynomial can be expressed bythe function

err(x) = (mx+ b) − sin(x). (1)

We know from the Equioscillation Theorem that the error between the Mini-max Polynomial and the sine curve will reach extrema at three points. Imaginingall possible lines, it seems clear that the maximum error will occur at the twoendpoints of the domain7 (at x = 0 and at x = π

2 ) and at some other x in thedomain. Let us call this third critical value x = q, where 0 < q < π

2 .Since we are looking for the polynomial with the minimum of possible max-

imum errors, we must find the point x = q at which the error function reachesits maximum value. When the error is at its maximum, the derivative of theerror function is zero and the second derivative is negative (when the slope ofthe error curve is zero and the error curve is concave down). To find this point,we take the derivative of equation (1) and set

err′(q) = 0.

Thusm− cos(q) = 0,

so thatq = arccos(m).

Thus we know that one of the points at which the error between our MinimaxPolynomial and the sine curve will reach its extremum is at x = arccos(m). Wewant to find the polynomial that makes the error at x = arccos(m) equal inabsolute value to the error at each of the two endpoints of the domain (at x = 0and at x = π

2 ).To find which values of m and b make up the linear Minimax Polynomial,

we set the three maximum errors equal in absolute value to one another. Theerror magnitude at x = 0 is∣∣err(0)

∣∣ = (0m+ b) − sin(0) = b,

the error magnitude at x = arccos(m) is∣∣err(arccos(m)

)∣∣ = sin(arccos(m)

)−[m(arccos(m)

)+ b

],

and the error magnitude at x = π2 is∣∣err(π2 )

∣∣ =(m(π2 ) + b

)− sin(π2 ) = mπ

2 + b− 1.

(The order of subtraction alternates so that the sign of the actual errors al-ternate, in accordance with Chebyshev’s theorem.) Equating the three error

7I don’t know a rigorous proof for this.

15

Page 16: Computing Transcendental Functions - LSU Math

magnitudes gives us a system of two equations from which we can find valuesfor m and b:

b = sin(arccos(m)

)−

[m(arccos(m)

)+ b

](2)

b = m(π2 ) + b− 1. (3)

To solve for m, we can cancel out the bs in equation (3), add 1 to both sides,and divide by π

2 to getm = 2

π .

Now that we have the value m = 2π , we can find the value of b. Substituting

m = 2π into equation (2), adding b to both sides and dividing by 2 gives

b =sin

(arccos( 2

π ))− 2

π arccos( 2π )

2≈ 0.1052568312.

Now that we have values for m and b, we have arrived at the degree 1Minimax Polynomial for the sine curve over the domain 0 ≤ x ≤ π

2 :

p(x) = 2πx+ 0.10527.

16

Page 17: Computing Transcendental Functions - LSU Math

0.25 0.5 0.75 1 1.25 1.5

0.2

0.4

0.6

0.8

1 17

Page 18: Computing Transcendental Functions - LSU Math

Figure 2.1. Sine curve and linear Minimax Polynomial

Since the maximum error between p(x) and sin(x) is b, we know that themaximum error of the degree 1 Minimax Polynomial for the sine curve over thedomain 0 ≤ x ≤ π

2 is 0.10527. Not bad for a linear polynomial!

2.2 Quadratic Minimax Polynomials

The higher the degree, n, of a Minimax Polynomial the better the approxima-tion. This is because Chebyshev’s theorem allows us to find the polynomialapproximation that is the optimal of all polynomials of degree ≤ n.

Suppose we want to find the degree 2 Minimax Polynomial for the sine curveover the domain 0 ≤ x ≤ π

2 . In order to do this we must find the points atwhich the error reaches local extrema and set the errors at these points equalto one another. We know from Chebyshev’s Equioscillation Theorem that theerror between the degree 2 Minimax Polynomial and the sine curve will reachextrema at four points in the domain. Imagining the possible parabolas, itseems plausible8 that these extrema will occur at the endpoints of the domain(at x = 0 and at x = π

2 ) and at two additional points in the domain. Let us callthese two additional points x = w and x = z where 0 < w < z < π

2 . We knowthat the polynomial will be of the form p(x) = ax2 + bx+ c. The error betweenthis polynomial and the sine curve can be expressed by the function

err(x) = ax2 + bx+ c− sin(x).

The error function reaches its extrema whenever its first derivative is equalto zero and its second derivative is not equal to zero. The first derivative of theerror function is

err′(x) = 2ax+ b− cos(x).

By setting the derivative of the error function equal to zero and substitutingx = w and x = z, we have the following two equations:

2aw + b = cos(w) (4)

2az + b = cos(z). (5)

We also know that the maximum error will be equal to c since we have a maxi-mum error at x = 0. We want to set err(0) = −err(w) = err(z) = −err(π2 ). Sowe have:

c = sin(w) − (aw2 + bw + c) = az2 + bz + c− sin(z) = 1 − (π2

4 a+ π2 b+ c). (6)

From equations (4), (5) and (6), we have a system of five equations and fiveunknown variables. We can eliminate two variables by rewriting equations (4)

8Again, I don’t know a rigorous proof.

18

Page 19: Computing Transcendental Functions - LSU Math

and (5),

a =cos(z) − cos(w)

2z − 2w(7)

b =z cos(w) − w cos(z)

z − w, (8)

and then substituting into (6) to obtain the system:

0 = az2 + bz − sin(z), (9)

which comes from err(0) = err(z),

sin(w) − aw2 − bw = 1 − π2

4a− π

2b, (10)

which comes from −err(w) = −e(π2 ), and

c =sin(w) − aw2 − bw

2, (11)

which comes from err(0) = −err(w). Keeping the substitutions (7) and (8) inmind, equations (9), (10), and (11) are a system of three equations in the threeunknowns w, z, and c. But c does not appear in (9) and (10), so that gives usa system of two equations in two unknowns, which needs to be solved. (Afterdoing so, we obtain the value of c from (11)).

Solving (9) and (10) would be an exhausting (and perhaps impossible) ex-ercise. The following list of commands in Mathematica is a reasonable methodfor approximating the correct values.

In[1]:= << Graphics‘ImplicitPlot‘

::Note: When entering the command, put back-quotes around ‘ImplicitPlot‘

In[2]:= A := (Cos[z]-Cos[w])/(2z-2w)

::This is the value of the coefficient a in terms of w and z.

In[3]:= B:= (z*Cos[w]-w*Cos[z])/(z-w)

::This is the value of the coefficient b in terms of w and z.

In[4]:= E1 = Simplify[(2z-2w)(A*z^2+B*z-Sin[z])]

::Thus E1 == 0 corresponds to equation (9).

In[5]:= E2 = Expand[Simplify[4(2z-2w)

(Sin[w]-A*w^2-B*w-1+Pi^2*A/4+Pi*B/2)]]

19

Page 20: Computing Transcendental Functions - LSU Math

::Thus E2 == 0 corresponds to equation (10).

In[6]:= ImplicitPlot[{E1==0, E2==0}, {w, 0, Pi/2}, {z, 0, Pi/2}]

::This graph shows where the two equations intersect. The diagonal line z == wshould be ignored, since we must have w < z. 9

9The line appears because our definition of E1 and E2 has a (z−w) factor. The only reasonfor including that factor is that it makes some of the intermediate Mathematica output lessmessy. The user is encouraged to try these commands without including that factor.

20

Page 21: Computing Transcendental Functions - LSU Math

0.25 0.5 0.75 1 1.25 1.5

0.25

0.5

0.75

1

1.25

1.5

21

Page 22: Computing Transcendental Functions - LSU Math

Figure 2.2.1. Intersection when E1 == 0 and E2 == 0

We want to find for which a, b, and c the graphs of the errors of w and of zintersect. We find this by repeatedly zooming in on the graph, and each timeguessing the point of intersection.

In[7]:= ImplicitPlot[E1==0, E2==0}, {w, 0.361145, 0.361146},

{z, 1.13333, 1.13334}, AspectRatio->.8]

In[8]:= A/. {w->0.361145, z->1.13334}

::This will output the value for the coefficient a ≈ −0.331429.

In[9]:= B/. {w->0.361145, z->1.13334}

::This will output the value for the coefficient b ≈ 1.17488.

In[10]:= C := (Sin[w]-A*w^2-B*w)/2

::This is the value of the coefficient c from equation (11).

In[11]:= C/. {w->0.361145, z->1.13334}

::This will output the value for the coefficient c ≈ −0.0138649.

Now that we have values for a, b, and c, we know that the quadratic MinimaxPolynomial for the sine curve is:

p(x) = −0.331429x2 + 1.17488x− 0.0138649.

If we plot the error function, err(x) = [−0.331429x2+1.17488x+0.0138649]−sin(x), we can see that the errors at x = 0, x = w, x = z, and x = π

2 are equalin absolute value.

22

Page 23: Computing Transcendental Functions - LSU Math

0.25 0.5 0.75 1 1.25 1.5

-0.01

-0.005

0.005

0.01

23

Page 24: Computing Transcendental Functions - LSU Math

Figure 2.2.2. Error of quadratic Minimax to sine curve

Since the error is equal to the coefficient c, we know that the maximumerror between this polynomial and the sine curve is 0.0138649. Compare this toa degree 2 Taylor approximation.10

Figure 2.2.3 shows our Minimax Polynomial and the sine curve. We can seethat there are four points at which the distance between the polynomial and thesine curve is maximized. We can also see that, in accordance with Chebyshev’stheorem, the errors at these points oscillate in sign and are equal in magnitude.

10There are many degree 2 Taylor approximations since we have to pick a point of expansion.Centered at x = 0, the degree 2 Taylor polynomial is the same as the degree 1 Taylor—sincesin(x) is an odd function—so it is just y = x, whose maximum deviation from sin(x) over0 ≤ x ≤ π

2is 0.570796.

24

Page 25: Computing Transcendental Functions - LSU Math

0.25 0.5 0.75 1 1.25 1.5

0.2

0.4

0.6

0.8

125

Page 26: Computing Transcendental Functions - LSU Math

Figure 2.2.3. Sine curve and quadratic Minimax

3 CoOrdinate Rotation DIgital Computer (CORDIC)Approximations

The final method that we will consider for computing the sine function is anapproach that is quite different from the polynomial methods described earlier.This approach is called Coordinate Rotation Digital Computer . As its namesuggests, this method for computing transcendental functions involves an algo-rithm for rotating the coordinates of an angle. By starting with an angle α = 0and rotating the coordinates of the angle by a series of smaller and smaller pos-itive or negative angles, we can efficiently bring the angle α arbitrarily close toany angle between 0 and π

2 . By this algorithm, we can arrive at an accurateapproximation to many of the common transcendental functions.

The formula for rotating the coordinates (X,Y ) by an angle θ is given by

X ′ = X cos(θ) − Y sin(θ)

Y ′ = X sin(θ) + Y cos(θ),

where (X ′, Y ′) are the new coordinates after rotating by angle θ. We can factorout cos(θ) from both coordinates leaving

X ′ = cos(θ)(X − Y tan(θ)

)Y ′ = cos(θ)

(X tan(θ) + Y

).

Factoring out cos(θ) is not necessary for the computation. However, it will bemade clear shortly that doing so increases the overall computational efficiency.

The concept behind the CORDIC approximation is that by following a spe-cific series of angle rotations, one can arrive at an accurate approximation toany angle between 0 and π

2 . We begin by storing a table of values similar to thefollowing:

θ tan(θ) cos(θ)

arctan(20) 20 cos(arctan(20)

)arctan(2−1) 2−1 cos

(arctan(2−1)

)arctan(2−2) 2−2 cos

(arctan(2−2)

)arctan(2−3) 2−3 cos

(arctan(2−3)

)...

......

arctan(2−59) 2−59 cos(arctan(2−59)

)Table 3.1

Using this table of values, we can rotate the coordinates of an angle α by aseries of positive or negative increments of arctan(2−i) which allows us to honein on any angle between 0 and π

2 . We choose to store values using exponents

26

Page 27: Computing Transcendental Functions - LSU Math

of base 2 only because the binary computation of such increments involves asimple “bitshift” operation rather than a multiplication or division. We canperform the angle rotations using the rotation formula, and once each angle inthe table has been either added or subtracted, we arrive at the approximatecoordinates for the angle we are interested in. Since every angle in the table isused exactly once, the rotation formula shows that each coordinate is multipliedby cos(θ) exactly once for each angle θ in the table. Since cos(θ) = cos(−θ),it does not matter whether we are rotating clockwise or counterclockwise—thecosine factor remains the same. This means that we can factor out cos(θ) fromthe rotation formula, store the product of the cosines, and multiply the finalcoordinates by this cosine product. Storing the product of the cosines allows usto perform the computation without multiplying cos(θ) to the coordinates aftereach rotation, thereby adding efficiency to the process. Since the coordinates(X,Y ) of the angle α, as a point on the unit circle, are equal to (cosα, sinα),it is clear that we can use this method to solve for sine, cosine, and tangent. Itturns out that variations of this method can be used to compute other commontranscendental functions as well. As before, we will illustrate the CORDICmethod by concentrating on the sine function.

3.1 An Example Using CORDIC Approximation

Suppose we want to compute sin(−21) using the CORDIC approach. Beforewe begin, we must find the sine input within the domain 0 ≤ x ≤ π

2 that willoutput a value from which we can infer sin(−21). The reasons for limiting thesine curve to this domain were explained earlier, in the discussion of Taylorapproximations (section 1). Since our input is a negative number, we start byfinding a non-negative input value that has a sine output that is equivalentto ± sin(−21). We can find such an input value easily by multiplying −21 by−1 and adding π. Then the input that we have to work with is 21 + π whichis approximately 24.142. Next we find the input between 0 and 2π that hasan output from which we can infer sin(24.142). We can find this x-value byrepeatedly subtracting 2π from 24.142 until we get a value between 0 and 2π.We can achieve the same result if we take the integer part of 24.142/2π, whichis 3, and subtract 2π times that value from our input:

24.142 − 6π = 5.292.

Now the input that we have to work with is 5.292. Next, we shift the domainto 0 ≤ x ≤ π. Since 5.292 is between π and 2π, and the sine is negative inthis domain, we make a note to negate our final solution. We can now shift theinput into the domain 0 ≤ x ≤ π by subtracting π from our input which leaves

5.292 − π = 2.15.

Now we can limit our domain for the final time to 0 ≤ x ≤ π2 . We do this by

reflecting about the line x = π2 , in other words, by changing our input, 2.15, to

π minus that inputπ − 2.15 = 0.9907.

27

Page 28: Computing Transcendental Functions - LSU Math

The number that we will work with for the remainder of the calculation is0.9907. Once we have computed the sine of 0.9907, we simply negate our solutionand the resulting value will be sin(−21). For the purposes of this example, itwill be sufficient to calculate the sine using a shortened table of angle rotations.By the end of this illustration, it should be clear that a longer table is likelyto make the approximation more accurate. The following is a table for thefirst five angle rotations. The table can be extended by following the simpleincrementation pattern.11

θi tan(θi) cos(θi)

arctan(20) = 0.785 20 cos(arctan(20)

)= 0.7602

arctan(2−1) = 0.4636 2−1 cos(arctan(2−1)

)= 0.8944

arctan(2−2) = 0.245 2−2 cos(arctan(2−2)

)= 0.9701

arctan(2−3) = 0.1244 2−3 cos(arctan(2−3)

)= 0.9923

arctan(2−4) = 0.0624 2−4 cos(arctan(2−4)

)= 0.9981

cosine product for first five rotations: = 0.6533

Table 3.1.1

Now we will step through this table, rotating by each angle θ and arrivingat more and more accurate approximations to the coordinates of 0.9907. Foreach element in the table, if our angle approximation is less than 0.9907, we willrotate in the positive direction (counterclockwise). Otherwise, we will rotatein the negative (clockwise) direction. We are going to begin the rotations atα0 = 0. To avoid confusion, it is important to keep in mind that the angle αrefers to the angle that is being rotated to approximate 0.9907, and the angleθi is the angle in the table at position i by which we rotate the angle α. Weknow that, on the unit circle, when we have an angle α = 0, the correspondingcoordinates of the point on the circle are (1, 0). This means that when we beginthe CORDIC approximation, we have

α0 = 0

X-coordinate = 1

Y -coordinate = 0.

(1) The first angle in our table is θ0 = arctan(20), or arctan(1), which isapproximately 0.785. Since our initial approximation is α0 = 0, and our desiredangle is 0.9907, we will rotate the coordinates of α in the positive direction.Once again, the rotation formula without the cos(θ) factor is

X ′ =(X − Y tan(θ)

)Y ′ =

(X tan(θ) + Y

),

11These are approximate decimal values. A computational version should store many moredecimal digits than shown in these tables so as to achieve higher precision in the final answer.

28

Page 29: Computing Transcendental Functions - LSU Math

where (X ′, Y ′) are the new coordinates after rotating by angle θ. SubstitutingX = 1, Y = 0, and θ0 = 0.785, gives

X ′ =[1 − 0

(tan(0.785)

)]= 1

Y ′ =[1(tan(0.785) + 0

)]≈ 1

So our new coordinates after the first rotation are (1, 1) and our new angleapproximation is α1 = 0.785.

(2) Now we refer back to our stored table of values. We see that the secondangle in the table is θ1 = arctan(2−1), or arctan( 1

2 ), which is approximately0.4636. Since our current approximation is α1 = 0.785, and our desired angleis 0.9907, we will again rotate our coordinates in the positive direction. Substi-tuting X = 1, Y = 1, and θ1 = 0.4636 into the rotation formula gives

X ′ =[1 − 1

(tan(0.4636)

)]≈ 0.5

Y ′ =[1(tan(0.4636) + 1

)]≈ 1.5.

So our new coordinates after the second rotation are (0.5, 1.5) and our new angleapproximation is α2 = 0.785 + 0.4636 ≈ 1.24905.

(3) The third angle in our table is θ2 = arctan(2−2), or arctan( 14 ), which

is approximately 0.245. Since our current approximation is α2 = 1.2486, andour desired angle is 0.9907, this time we will rotate in the negative direction, orby −0.245. Substituting X = 0.5, Y = 1.5, and θ2 = −0.245 into the rotationformula gives

X ′ =[0.5 − 1.5

(tan(−0.245)

)]≈ 0.875

Y ′ =[0.5

(tan(−0.245) + 1.5

)]≈ 1.375.

So our new coordinates after the third rotation are (0.875, 1.375) and our newangle approximation is α3 ≈ 1.2495 − 0.245 = 1.00407.

We can see already that we are begining to hone in on our desired angle,0.9907. If we were to continue in this fashion, we would rotate by the fourth an-gle in the negative direction. This would bring us to α4 = 1.00407−arctan(2−3)which is approximately 0.879712. After that we would rotate by the fifth anglein the positive direction bringing us to α5 = 0.879712 + arctan(2−4) which isapproximately 0.942131. If we were to continue down an extended table, wewould rotate the coordinates in either the positive or negative direction, usingeach angle on the list exactly once. When we have completed every rotationin the list, we multiply the Y -coordinate by the cosine product and negate thisresult to get the value of sin(−21).12 Listed below are the angle and coordinate

12Multiplying the X-coordinate by the cosine product will give the value of cos(0.9907), butthis is not the same as cos(−21). Recall that we arrived at 0.9907 by shifting the sine curveinto the range 0 ≤ x ≤ π

2.

29

Page 30: Computing Transcendental Functions - LSU Math

approximations that would be computed to approximate sin(−21) if the tablecontains 15 entries.

angle approximation x-coordinate y-coordinateα1 ≈ 0.785398 X = 1 Y ≈ 1α2 ≈ 1.24905 X ≈ 0.5 Y ≈ 1.5α3 ≈ 1.00407 X ≈ 0.875 Y ≈ 1.375α4 ≈ 0.879712 X ≈ 1.04688 Y ≈ 1.26562α5 ≈ 0.942131 X ≈ 0.967773 Y ≈ 1.33105α6 ≈ 0.973371 X ≈ 0.926178 Y ≈ 1.3613α7 ≈ 0.988994 X ≈ 0.904908 Y ≈ 1.37577α8 ≈ 0.996807 X ≈ 0.89416 Y ≈ 1.38284α9 ≈ 0.992901 X ≈ 0.899561 Y ≈ 1.37935α10 ≈ 0.990947 X ≈ 0.902255 Y ≈ 1.37759α11 ≈ 0.991924 X ≈ 0.90091 Y ≈ 1.37847α12 ≈ 0.991436 X ≈ 0.901583 Y ≈ 1.37803α13 ≈ 0.991192 X ≈ 0.901919 Y ≈ 1.37781α14 ≈ 0.99107 X ≈ 0.902088 Y ≈ 1.3777α15 ≈ 0.991131 X ≈ 0.902004 Y ≈ 1.37776

cosine product for 15 rotations: = 0.607253sine approximation: 1.37776 ∗ 0.607253 ∗ −1 = −0.8366489

Table 3.1.2

In longer lists of angle rotations, the angle approximation αi might actuallyhit our desired angle. It is important, however, that the computation proceedthrough the final angle rotation in the table. This is because we factored outcos(θi) from the rotation formula and pre-computed the product of a particularnumber (the length of the table) of these cosine factors. By increasing the lengthof the table of angles, we can obtain an arbitrarily accurate approximation tothe coordinates of a chosen angle. After using each angle in the table, we simplymultiply both coordinates by our cosine product. At the end, some cleanup work(for example, negating the Y -coordinate, as in our previous example) finishesthe calculation.

The following is a C++ implementation of the CORDIC method for com-puting the sine function.13

3.2 C++ Program Code for CORDIC

#include <iostream.h>

#include <math.h>

13Note: The initialize () routine is only called once. It computes the table of values andthe cosine product used by CORDIC. In keeping with the spirit of this paper, the math librarycalls in this routine ought to be replaced with the appropriate computations—for example,since efficiency does not matter here, ridiculously high degree Taylor Polynomials could beused. The manner in which one precomputes the table has nothing to do with CORDIC, sowe kept this part of the code as short as possible.

30

Page 31: Computing Transcendental Functions - LSU Math

const double pi = 4*atan(1);

const int tableSize = 60;

double angles[tableSize]; //array that stores angles

double twopower[tableSize]; //array of tangents of angles

double initialize()

//iteratively fills array twopower with 2^-i

//iteratively fills array angles with

//arctan(twopower[i])

{

double cosineProduct = 1; //product of cosines starts at 1

for (int i = 0; i < tableSize; i++)

{

twopower[i] = exp(-i*log(2.0));

angles[i] = atan(twopower[i]);

cosineProduct *= cos(angles[i]);

}

return cosineProduct;

}

double limitDomain(int flag, double input)

//sets the value of input to its equivalent

//within domain 0<=x<=Pi/2.

{

if(input < 0) //sets 0<=input<infinity

input = pi - input;

input -= 2*pi*(long int)(input/(2*pi)); //sets 0<=input<=2Pi

if(input > pi) //sets 0<=input<=Pi

{

input -= pi;

flag = -1; //sines in here are negative

}

if(input > pi/2) //sets 0<=input<=Pi/2

input = pi - input;

return flag * input;

}

double rotateCoords(double number)

//initializes flag, angle, x, and y.

//rotates coordinates and adds or subtracts angle for

//each element in array angles.

{

int flag = 1; //flag for negation of solution

double angle = 0; //current angle approx to number

double x = 1; //X-coordinate

double y = 0; //y-coordinate

31

Page 32: Computing Transcendental Functions - LSU Math

double cosineProduct = initialize();

double num = limitDomain(flag, number);

if(num < 0)

flag = -1;

num *= flag;

for(int j = 0; j < tableSize; j++)

{

double newx = x;

double newy = y;

if(angle < num) //positive rotation

{

newx -= y * twopower[j];

newy += x * twopower[j];

angle += angles[j];

}

else //negative rotation

{

newx += y * twopower[j];

newy -= x * twopower[j];

angle -= angles[j];

}

x = newx;

y = newy;

}

x *= cosineProduct; //multiply coords by the cosines

y *= cosineProduct; //that were factored out

return y *= flag;

}

int main()

{

char response;

double number;

cout<<"Please enter (s)ine, or press any other key to quit: ";

cin>>response;

while(response == ’s’)

{

cout<<"Please enter the number: ";

cin>>number;

double num = number;

cout<<"The sine of "<<num<<" is: "<<rotateCoords(number)<<endl;

cout<<endl;

cout<<"Please enter (s)ine, or press any other key to quit: ";

cin>>response;

32

Page 33: Computing Transcendental Functions - LSU Math

}

return 0;

}

SAMPLE RUN

==========

Please enter (s)ine, or press any other key to quit: s

Please enter the angle (in radians): -21

The sine of -21 is: -0.836656

Please enter (s)ine, or press any other key to quit: s

Please enter the angle (in radians): 9.32

The sine of 9.32 is: 0.104586

Please enter (s)ine, or press any other key to quit: s

Please enter the angle (in radians): -5.1

The sine of -5.1 is: 0.925815

Please enter (s)ine, or press any other key to quit: m

33