<<

[dsp TIPS&TRICKS] Sreeraman Rajan, Sichun Wang, Robert Inkol, and Alain Joyal

Efficient Approximations for the Arctangent

his article provides several Iterative algorithms, such as the required division operations are rela- efficient approximations for coordinated rotation digital computer tively complex to implement; iterative the arctangent function (CORDIC) algorithm (requiring only techniques, such as those based on using Lagrange interpolation shifts and add operations), have been Newton’s method, are often used. and minimax optimization successfully used to compute trigono- The aforementioned approaches are Ttechniques. These approximations are metric functions [1], [2]. However, best suited for applications where pro- particularly useful when processing their sequential nature makes them cessing power and/or memory are read- power, memory, and power consumption less attractive when speed is a major ily available. However, for many are important issues. In addition to com- concern; attempts to increase speed applications, simpler and more effi- paring the errors and the computational have been at the expense of additional cient ways of evaluating arctan(x) are workload of these approximations, we hardware [3]. Lookup-table-based desirable. This article proposes simple also extend them to all four quadrants. approaches to the computation of approximations for evaluating the arct- inverse are angent function that may be easily ARCTANGENT APPROXIMATIONS very fast but require considerable implemented in hardware with limited The evaluation of the arctangent func- memory [4], [5]. memory and processing power. tion is commonly encountered in real- Polynomial and rational function time signal processing applications. approximations that have been pro- PROPOSED METHODOLOGY Numerous algorithms are available to posed in the literature [3] are more AND APPROXIMATIONS implement the arctangent function suitable for numerical coprocessors. Approximations to the arctangent when computational cost is unimpor- Approximations using polynomials of function can be obtained using second- tant. The most direct solution is based large degrees are computationally and third-order polynomials and on the Taylor series expansion on [−1, expensive. Rational approximations are simple rational functions. Lagrange 1]. However, this series converges in principle more accurate than polyno- interpolation-based and minimax slowly for arguments close to one and mial approximations for the same num- criterion-based approaches are used to hence is inefficient. ber of coefficients. However, the obtain the polynomial coefficients. The following is our initial derivation of an arctangent approximation using Lagrange interpolation. Consider the three points x0 =−1, × 10−2 = = ( ) = 8 x1 0, and x2 1. Let x arctan (1 + x)/(1 − x), −1 ≤ x ≤ 1. According to the Lagrange interpolation formula, we have 4

(x − x1)(x − x2) φ(x) ≈ φ(x0) (x0 − x1)(x0 − x2) 0 (x − x0)(x − x2) + φ(x1) Error (rad) (x1 − x0)(x1 − x2) ( − )( − ) −4 x x0 x x1 + φ(x2) (x2 − x0)(x2 − x1) π = (x + 1), −1 ≤ x ≤ 1.(1) −8 4 −1 0 1 x It can be verified that (x) = π/4 + arctan(x); hence, arctan(x) can be [FIG1] Approximation errors using (2). approximated by the first-order formula

IEEE SIGNAL PROCESSING MAGAZINE [108] MAY 2006 1053-5888/06/$20.00©2006IEEE π The above criterion (6) [TABLE 1] MAXIMUM ERROR arctan(x) ≈ x, −1 ≤ x ≤ 1.(2) 4 does the following: for AND COMPUTATIONAL WORKLOAD. every α, the maximum EQUATION ERROR (RAD.) ADDS MULTIPLIES DIVIDES This linear approximation has been used absolute error between (2) 0.07 0 1 0 (5) 0.0053 1 2 0 in [6] for FM demodulation due to its (x) and the second- (7) 0.0038 1 2 0 minimal complexity. It requires only a order polynomial (8) 0.005 1 3 0 2 (9) 0.0015 2 3 0 scaling operation by a fixed constant and (αx − αx) is deter- (10) 0.0047 1 2 1 can be computed in one cycle in most mined. This error, as a (11) 0.0049 2 1 1 processors. Figure 1 shows the deviation function of α, is then of this linear (first-order) approximation minimized. This will from the arctangent function on the yield a unique α that interval [−1, 1]. This error function, gives the least error J; hence the name A better third-order polynomial for given in (3), is antisymmetric with respect minimax criterion, and the approxima- approximating the error is of the form to x = 0 and attains a maximum of about tion is called minimax approximation. x(x − 1)(αx − β) for x in the interval 1/2 0.07 rad (4º) at xmax =±(4/π − 1) Using an extensive computer search, [0,1]. Using the minimax criteria, the fol- the optimum α ≈ 0.273 and the follow- lowing polynomial with a maximum π (x) = arctan(x) − x, ing approximation for the arctan(x) absolute error of 0.0015 rad (0.086º) is 4 with a maximum absolute error of identified as the optimal approximation − 1 ≤ x ≤ 1.(3) 0.0038 rad (0.22º) are obtained: to arctan(x)

π Here’s a useful trick to improve the arctan(x) ≈ x + 0.273x(1 −|x|), π accuracy of the first-order approximation 4 arctan(x) ≈ x − x(|x|−1) (2). It can be seen from Figure 1 that the − 1 ≤ x ≤ 1.(7) 4 × ( . + . | |), error curve is approximately quadratic 0 2447 0 0663 x on the interval [0, 1] and hence can be A third-order polynomial is also a − 1 ≤ x ≤ 1.(9) approximated by a second-order polyno- good candidate to fit the error curve mial. Following this strategy and apply- shown in Figure 1. When the third- ing Lagrange’s interpolation formula to order polynomial is constrained to be Another candidate for approximating 3 (x) using x0 = 0, x1 = xmax , and of the form αx + β x, the best mini- the arctangent function is from the x2 = 1, we obtain the following formula: max approximation for the arctangent class of rational functions of the form function is given by τ(x) = x/(1 + β x2) in the interval (x) ≈ 0.285x(1 −|x|), [−1, 1]. For 0 ≤ β ≤ 1, the first deriva- − 1 ≤ x ≤ 1,(4) arctan(x) tive of τ(x) is positive and the second π derivative of τ(x) is negative. This ≈ x + x(0.186982 where the odd symmetry of (x) has 4 implies that τ(x) has a shape very simi- been applied. A second-order, and more − 0.191942x2), −1 ≤ x ≤ 1.(8) lar to that of the arctangent function accurate, approximation for arctan(x) in the same interval. Using minimax with a maximum absolute error of The maximum absolute error for this criteria, the following approximation 0.0053 rad (0.3º) is thus obtained as approximation is 0.005 rad (0.29º) and is with the maximum absolute error of worse than that given by (7). about 0.0047 rad (0.27º) is obtained: π arctan(x) ≈ x + 0.285x(1 −|x|), 4 − 1 ≤ x ≤ 1.(5)

It is also of interest to approximate × 10−3 6 the error function, (x), given by (3), by Eq. (5) 4 a second-order polynomial of the form Eq. (11) 2 (αx2 − αx) that passes through the end- 0 points of the interval [0, 1]. The optimum −2 α>0 Error (rad) Eq. (7) parameter, , may be obtained using −4 the following minimax criterion −6 −1 −0.5 0 0.5 1 x J = min max {|(x) − αx(x − 1) |} . α 0≤x≤1 (6) [FIG2] Approximation errors using second-order polynomials (5), (7) and a rational (11).

IEEE SIGNAL PROCESSING MAGAZINE [109] MAY 2006 [dsp TIPS&TRICKS] continued

information of the argument is already available. However, one needs to check × 10−3 the of the argument, which may 5 Eqs. (10),(11) Eq. (8) take an extra cycle. Comparison of the approximations to arctan(x) using the proposed two 0 second-order approximations given by (5)

Error (rad) and (7) are shown in Figure 2. These Eq. (9) −5 approximations have maximum errors −1 −0.5 0 0.5 1 that are an order of magnitude better x than that of the linear approximation (2). Furthermore, the second-order approxi- [FIG3] Approximation errors using cubic polynomials (8), (9) and rationals (10), (11). mation given by (5) provides better accu- racy for the subintervals 1 > x > 0.5 and −1 < x < −0.5, where the maximum error is only about 0.001 rad (0.057º). No x arctan(x) ≈ , DISCUSSION such observation can be made for (7). 1 + 0.28086x2 Here we compare the various arctan- Figure 3 shows that (9) provides − 1 ≤ x ≤ 1.(10) gent approximations presented in this the best accuracy amongst the third- article. Table 1 contains the maximum order approximations considered in error and the number of adds, multi- this article, while the rational approxi- Recently, a similar idea was present- plies, and divides for various arctan mations (10) and (11) have essentially ed in [5] with a maximum absolute approximations. The third-order identical errors. The error approaches error of 0.0049 rad (0.28º). The approxi- approximation given by (9) has the zero at x =±1 for (4), (7), and (9) (see mation in [5] is least error among the proposed approx- Figures 2 and 3). imations but has the highest computa- The rational approximations given by tional cost. The second-order (10) and (11) are computationally more x arctan(x) ≈ , approximation given by (7) has the next expensive than the second-order ones 1 + 0.28125x2 lowest error and has fewer operations given by (5) and (7), though this cost − 1 ≤ x ≤ 1.(11) than (9). Hence, (7) provides a favor- increase is partly offset by the elimination able compromise between accuracy and of the need for sign comparisons. computational cost. The linear approxi- Approximations using (10) or (11) have a The scaling constant, 0.28125, was mation given by (2) has only one multi- division operation that may slow down chosen to permit multiplication to be plication operation but has the least the processing. The approximation using performed (as discussed later) with accuracy. With a single-cycle multiply (11) and the approximation given by (5) two arithmetic binary right shifts and and accumulate (MAC) processor, the intersect at approximately xc = 0.3933 in a single addition. This yields a negligi- evaluation of the arctangent using (7) the interval [0, 1]. For values of |x| > xc, ble increase in maximum error. would take only two cycles if the sign the proposed approximation given by (5) yields better accuracy as shown in Figure 2. For values of |x| < xc, the approxima- [TABLE 2] ARCTAN APPROXIMATIONS VERSUS OCTANT LOCATIONS. tion in (11) gives better accuracy. An alternative approach to improving OCTANT APPROXIMATION (7) APPROXIMATION (10) the accuracy, though at the expense of computational cost, would be to combine Q Q/I I, VIII [1.0584 − sign(Q) · 0.273 · (Q/I)] (5) and (11). This combined equation is I Q2 1 + 0.28086 · given by I2 π I π I/Q II, III − [1.0584 − sign(I) · 0.273 · (I/Q)] − arctan(x) ≈ γ A + (1 − γ)B,(12) 2 Q 2 I2 1 + 0.28086 · Q2 Q Q/I where A is given by (11) and B is given by IV, V sign(Q).π + [1.0584 + sign(Q) · 0.273 · (Q/I)] sign(Q) · π I Q2 (5). The weighting variable γ is unity 1 + 0.28086 · 2 I when 0 ≤|x|≤xc and zero otherwise. π I π I/Q VI, VII − − [1.0584 + sign(I) · 0.273 · (I/Q)] − − Using this approach, the maximum error 2 Q 2 I2 1 + 0.28086 · is less than 0.0025 rad (0.14º), as can be Q2 seen from Figure 2.

IEEE SIGNAL PROCESSING MAGAZINE [110] MAY 2006 FOUR QUADRANT APPROXIMATIONS CONCLUSION The arctan algorithms presented thus far Simple approximations to the arctan- are applicable for angles in the range of gent function and four quadrant arctan- −π/4 to π/4 rad. Here we extend the gent functions have been introduced. angular range to −π to π radians. Let The second-order polynomial in (7) pro- z = I + jQ be any and vides a favorable compromise between let x = Q/I. The four quadrant arctan- accuracy and computational cost. gent function atan2(I,Q) can be evaluated Furthermore, it is well suited for imple- over the four quadrants by substituting mentation in hardware. Q/I for x in the approximations presented in this article. Due to space limitations, ACKNOWLEDGMENTS we only consider the extensions for We would like to thank the associate edi- approximations given by (7) and compare tor, Rick Lyons, for helpful and construc- them to the four quadrant arctangent tive comments, suggestions, and go expressions given for (10). modifications. We would also express our To obtain the four quadrant arctangent gratitude to our colleagues, Dr. Fred. A. approximations, the range over which the Dilkes and Mr. Sean Stamplecoskie, for there. approximation operates is extended. The reviewing the article and suggesting complex is divided into eight octants improvements. where octant I, for example, covers the angle range of 0 to π/4 rad. The four quad- do rant calculations are thus reduced to the AUTHORS first octant calculations, and using the Sreeraman Rajan, Sichun Wang, Robert rotational symmetries of arctangent func- Inkol, and Alain Joyal are with Defence that. tion the approximations for the other Research and Development Canada octants can be easily obtained. Table 2 pro- (DRDC) in Ottawa, Canada. vides the four quadrant approximations based on (7) and (10). It should be noted that Table 2 uses the following definition REFERENCES for determining the sign value of an [1] W.F. Wong and E. Goto, “Fast hardware- shop.ieee.org based algorithms for elementary function com- argument: putations using rectangular multipliers,” IEEE Trans. Comput., vol. 43, no. 3, pp. 278–294, 1, z ≥ 0 Mar. 1994. sign(z) = (13) −1, z < 0. [2] D.D. Hwang, D. Fu, and A.N. Willson, “A 400- 30% of the world’s Many desirable features of the four quad- MHz processor for the conversion of rectangular to polar coordinates in 0.25-µm CMOS,” IEEE J. top-cited, peer-reviewed rant rational approximation based on Solid-State Circuits, vol. 38, no. 10, journals, standards, and pp. 1771–1775, Oct. 2003. (11) are given in [5]. The multiplication more, in electronic by 0.28125 in the denominator of (11) [3] I. Koren and O. Zinaty, “Evaluating elemen- can be implemented as a sum of two tary functions in a numerical coprocessor based engineering, computing arithmetic right shifts, x2/4 and x2/32. on rational approximations,” IEEE Trans. and related technologies Comput., vol. 39, no. 8, pp. 1030–1037, Aug. (This fact accounts for the two adds and 1990. — from the world’s single multiply requirements for (11) in largest technical Table 1.) The constants appearing in the [4] M.R.D. Rodriguez, J.H.P. Zurawski, and G.B. Gosling, “Hardware evaluation of mathematical professional society. four quadrant approximations using functions,” Proc. IEE, vol. 128, pt. E, no. 4, either (5) or (10) cannot be implement- pp. 278–294, July 1991. ed with shifts. However, the complexity [5] R. Lyons, “Another contender in the arctangent of the four quadrant arctangent approxi- race,” IEEE Signal Processing Mag., vol. 20, no. 1, mations using any of (5), (10), or (11) pp. 109–111, Jan. 2004. ShopIEEE are the same. The four quadrant arctan- gent approximation using (7) has better [6] J.M. Shima, “FM demodulation using a digital radio and digital signal processing,” M.Sc. thesis, accuracy and deserves consideration. University of Florida, Gainesville,1995. [SP]

IEEE SIGNAL PROCESSING MAGAZINE [111] MAY 2006