Xref: utzoo comp.lang.c:22414 comp.unix.questions:16689
Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!um-math!sharkey!cfctech!teemc!hpftc!zardoz!henry.jpl.nasa.gov!elroy.jpl.nasa.gov!usc!cs.utexas.edu!ico!ism780c!news
From: news@ism780c.isc.com (News system)
Newsgroups: comp.lang.c,comp.unix.questions
Subject: Re: fast arc tangent routine available?
Keywords: math library, atan(), atan2(), arc tangent, table lookup
Message-ID: <32488@ism780c.isc.com>
Date: 30 Aug 89 21:31:56 GMT
References: <6002@pt.cs.cmu.edu>
Reply-To: marv@ism780.UUCP (Marvin Rubenstein)
Distribution: na
Organization: Interactive Systems Corp., Santa Monica CA
Lines: 33

In article <6002@pt.cs.cmu.edu> aki@speech1.cs.cmu.edu (Yoshiaki Ohshima) writes:
>Hello, does anyone know if there are any 'arc tangent' routines, which run
>faster than atan() and atan2() of the standard C math library?
>
>I am now using Micro-Vax-II and (occasionally) PMAX, and have an impression
>that my simple-minded table lookup using bisection search doesn't seem to
>work well. Has anyone ever worked on this?
>
>						--aki

Since the table has to span an infinite range, table lookup does not seem to
be very effective.  Assuming you need only three or so decimal digits of
accuracy, here is a formula from Cecil Hastings, Approximations for Digital
Computers.  It works for all x between 0 and infinity.  If x is negative,
change the sign of x and the sign of the answer.  The maximum error in atan
is plus or minus .0005.

    let y = (x-1)/(x+1)
    let z = y*y

then
     atan(x) = .785398 + (.995354 + (-.288679 + .079331*z)*z)*y

This is a fairly quick method.  If you need to compute atan from inside a
loop, than write the above code inline to avoid subroutine call overhead.

     Hope this helps,
	Marv Rubinstein

PS: I leave it as an exercise for the reader to use this formula as the basis
for an atan2 routine.

PPS: The first term is pi/4 if you had not guessed.