jmasterx jmasterx - 20 days ago 12
C Question

Fast Arc Cos algorithm?

I have my own, very fast cos function:

float sine(float x)
{
const float B = 4/pi;
const float C = -4/(pi*pi);

float y = B * x + C * x * abs(x);

// const float Q = 0.775;
const float P = 0.225;

y = P * (y * abs(y) - y) + y; // Q * y + P * y * abs(y)


return y;
}

float cosine(float x)
{
return sine(x + (pi / 2));
}


But now when I profile, I see that acos() is killing the processor. I don't need intense precision. What is a fast way to calculate acos(x)
Thanks.

Answer

A simple cubic approximation, the Lagrange polynomial for x ∈ {-1, -½, 0, ½, 1}, is:

double acos(x) {
   return (-0.69813170079773212 * x * x - 0.87266462599716477) * x + 1.5707963267948966;
}

It has a maximum error of about 0.18 rad.