# Fast approximate arctan/atan function

While searching for a fast arctan approximation I came across this paper:

Efficient approximations for the arctangent function”, Rajan, S. Sichun Wang Inkol, R. Joyal, A., May 2006

Unfortunately I no longer have access to the IEEE papers (despite paying for yearly membership, what a joke …), but fortunately the paper appeared in a book that Google has for preview (for selected pages), “Streamlining digital signal processing: a tricks of the trade guidebook”. Even luckier, Google had the important pages on preview. The paper presents 7 different approximation, each with varying degree of accuracy and complexity.

Here is one algorithm I tried, which has a reported maximum error 0.0015 radians (0.085944 degrees), lowest error in the paper.

```double FastArcTan(double x)
{
return M_PI_4*x - x*(fabs(x) - 1)*(0.2447 + 0.0663*fabs(x));
}```

The valid range for x is between -1 and 1. Comparing the above with the standard C atan function for 1,000,000 calls using GCC gives:

 Time ms FastArcTan 17.315 Standard C atan 60.708

About 3x times faster, pretty good!

## 9 thoughts on “Fast approximate arctan/atan function”

1. jirkamat says:

Hi, thanks for sharing this. Helped me to find out that atan function is correct on my GPU in OpenCL. I tested this because another function, tan, turned out to be buggy on this GPU.

1. nghiaho12 says:

Glad you found it useful ðŸ™‚

2. Garry says:

Thanks for posting this, it’s very useful if you don’t have time for the standard C function but can deal with a small accuracy loss!

3. Tim says:

What is M_PI_4?

Thanks for the resource!

1. nghiaho12 says:

PI/4

1. Tim says:

Ahh makes sense. I figured, just didn’t want to assume.
Thanks once more, will use this in some sand dune simulations!

1. Tim says:

Oh and one more thing, as far as speed is concerned, if you make this a macro (using #DEFINE in C/C++) it’ll theoretically be even faster than your standard function (or making it inline should have similar effect).

1. nghiaho12 says:

Depends. The compiler may be smart enough to inline such a small function. But doesn’t hurt to be explicit I guess.