From: kovarik@mcmail.cis.McMaster.CA (Zdislav V. Kovarik) Subject: Re: to arctan or not to arctan Date: 23 Dec 1999 11:53:36 -0500 Newsgroups: sci.math Keywords: numerical calculation of arctangent function In article <38624091_2@bingnews.binghamton.edu>, Bob Riley wrote: :Richard Parsons wrote: :> Lo all : :> Hope someone can help here... my problem is with arctan... well, more :> the lack of arctan. I am writing software in an embedded computer and :> all the functions I have are sin,cos and tan and I need to calculate :> Theta from 2 known points (This is for a GPS thingy, you enter your :> destination Latitude/Longitude and this gizmo should tell you what your :> heading should be to get to your destination) : :> Can anyone post a fix for me (formula not using arctan) :> or posting a simplified arctan formula (precision is not that critical) : : You seem not to have ANY inverse trig functions. : : A dirty fix: from tan(a+b) = (tan(a)+tan(b))/(1-tan(a)*tan(b)) I get :tan(t/2)=sqrt(1+tan(t))-1. (Check my work!) A cleanup: tan(t/2) = tan(t) / (1 + sqrt(1 + (tan(t))^2) (the earlier formula fails a quick check for t = -pi/4, and for t = pi/3. Remark (skip if fine points of round-off are not your priority): A dirty version of this formula, algebraically equivalent when tan(t) is not 0, would be tan(t/2) = (sqrt(1 + (tan(t))^2) - 1) / tan(t) , but for small values of tan(t) you lose digits of accuracy -- the smaller tan(t), the worse the loss due to subtraction in the numerator). End of remark. >Then repeat: >tan(t/4)=sqrt(1+tan(t/2))-1, tan(t/8)= ..., till you get tan(t/2^n) --- (with appropriate changes) >which is less than, say, 1/1000. Now use > > arctan(x) = x - x^3/3 + x^5/5 - x^7/7 + ... > >when abs(x) < 1: your x would be the computed tan(t/2^n). The first >two terms of the series should be t/2^n to your required accuracy. Then >multiply by 2^n to get t=arctan(tan(t)). Another approach, as you got a rough estimate t of the solution of tan(t) ~= a is using an improvement formula t_new = t - (tan(t) - a) / (1 + a * tan(t)) A re-arrangement in two stages: Calculate r = tan(t) - a improve t_new = t - r / (1 + a * (a + r)) (and then replace t with t_new) Students of numerical analysis will recognize this as Newton's Method applied to the equation sin(t) - a * cos(t) = 0. More patient students will verify that the convergence is here better than quadratic, namely cubic. The number of accurate digits will roughly triple after each improvement step. Cheers, ZVK(Slavek). ============================================================================== From: Kurt Foster Subject: Re: to arctan or not to arctan Date: Thu, 23 Dec 1999 18:29:37 GMT Newsgroups: sci.math In <38623703.350853AF@cellpt.co.za>, Richard Parsons said: [snip] . Can anyone post a fix for me (formula not using arctan) or posting a . simplified arctan formula (precision is not that critical) An 33-year-old IBM manual gives the following algorithm: First, use the relations Arctan(-x) = - Arctan(x) and Arctan(1/|x|) = pi/2 - Arctan(|x|), if necessary, to reduce the argument to the range, 0 =< x =< 1. Then, if necessary, use the relation Arctan(x) = pi/6 + Arctan((x*sqrt(3) - 1)/(sqrt(3) + x)) to reduce to the interval -tan(pi/12) =< x Subject: Re: to arctan or not to arctan Date: Mon, 27 Dec 1999 15:33:34 GMT Newsgroups: sci.math In , daan Strebe said: . Richard Parsons writes: .. I am writing software in an embedded computer and all the functions I . have are sin,cos and tan . Are you entirely certain you have "tan" in your software libraries, . rather than "atan"? Libraries stripped of anything superfluous supply . sine, cosine, and arctangent because all the other trigonometric . functions and their inverses are readily computable from those three. [snip] I think you may well be right. The algorithm for arctangent I posted was from a 33-year-old IBM Fortran manual, and described the library function ATAN. The library wasn't quite a "minimal" library, but it did have sin() cos() and atan() [and their double precision counterparts] - but no other trig or inverse trig functions. I can't imagine a library that has tan() but NO inverse trig functions. Fer krissakes - even QBASIC had an inverse tangent function! ============================================================================== From: udaypatil@home.com (Uday Patil) Subject: to arctan or not to arctan Date: 28 Dec 1999 14:11:41 -0500 Newsgroups: sci.math That is a beautiful algorithm, though I would use Newton's iteration for the last step (extending the algorithm to arbitrary precision). Regarding the correct quadrant, I strongly recommend (through personal experience) attempting to solve for half the angle: -PI/2 < thata/2 <= PI/2 . The essence of this trick lies in (analytically) expressing tan(theta/2) in terms of the same variables used to express tan(theta). In most cases you end up in a simple expression which allows you to pick the right sign (for the square root), thus eliminating the quandrant-degeneracy introduced by taking the ratio of sin(theta) to cos(theta). If you have access to sin(theta) and cos(theta), then simply solve for tan(theta/2) = sin(theta) / (1 + cos(theta) ) i.e. theta = 2 arctan( sin(theta) / (1 + cos(theta) ) ) Uday [previous post quoted --djr]