Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 710
jmasterx Avatar asked Aug 01 '10 03:08

jmasterx


People also ask

What is ARC Cos formula?

In a right-angled triangle, the cosine of an angle (θ) is the ratio of its adjacent side to the hypotenuse. i.e., cos θ = (adjacent side) / (hypotenuse). Then by the definition of arccosine, θ = cos-1[ (adjacent side) / (hypotenuse) ] .

What is arc cosine used for?

The arccosine is the inverse of the cosine. It's most useful when trying to find the angle measure when two sides of a triangle are known. The arccosine allows you to find the measure of an angle when you know the ratio of the adjacent side to the hypotenuse.

How do you use inverse trig functions in C?

C acos() The acos() function returns the arc cosine (inverse cosine) of a number in radians. The acos() function takes a single argument (1 ≥ x ≥ -1), and returns the arc cosine in radians. Mathematically, acos(x) = cos-1(x) .


2 Answers

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.

like image 63
dan04 Avatar answered Oct 17 '22 05:10

dan04


Got spare memory? A lookup table (with interpolation, if required) is gonna be fastest.

like image 27
spender Avatar answered Oct 17 '22 05:10

spender