# [Mesa-dev] [PATCH] glsl: improve accuracy of atan()

Erik Faye-Lund kusmabite at gmail.com
Fri Oct 10 03:30:10 PDT 2014

```On Fri, Oct 10, 2014 at 12:22 PM, Timothy Arceri <t_arceri at yahoo.com.au> wrote:
> On Mon, 2014-10-06 at 17:03 +0200, Erik Faye-Lund wrote:
>> On Fri, Sep 26, 2014 at 6:11 PM, Erik Faye-Lund <kusmabite at gmail.com> wrote:
>> > Our current atan()-approximation is pretty inaccurate at 1.0, so
>> > let's try to improve the situation by doing a direct approximation
>> > without going through atan.
>> >
>> > This new implementation uses an 11th degree polynomial to approximate
>> > atan in the [-1..1] range, and the following identitiy to reduce the
>> > entire range to [-1..1]:
>> >
>> > atan(x) = 0.5 * pi * sign(x) - atan(1.0 / x)
>> >
>> > This range-reduction idea is taken from the paper "Fast computation
>> > of Arctangent Functions for Embedded Applications: A Comparative
>> > Analysis" (Ukil et al. 2011).
>> >
>> > The polynomial that approximates atan(x) is:
>> >
>> > x   * 0.9999793128310355 - x^3  * 0.3326756418091246 +
>> > x^5 * 0.1938924977115610 - x^7  * 0.1173503194786851 +
>> > x^9 * 0.0536813784310406 - x^11 * 0.0121323213173444
>> >
>> > This polynomial was found with the following GNU Octave script:
>> >
>> > x = linspace(0, 1);
>> > y = atan(x);
>> > n = [1, 3, 5, 7, 9, 11];
>> > format long;
>> > polyfitc(x, y, n)
>> >
>> > The polyfitc function is not built-in, but too long to include here.
>> >
>> > http://www.mathworks.com/matlabcentral/fileexchange/47851-constraint-polynomial-fit/content/polyfitc.m
>> >
>> > This fixes the following piglit test: