C acos()

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

The acos() function is included in <math.h> header file.


acos() Prototype

double acos(double x);

To find arc cosine of type int, float or long double, you can explicitly convert the type to double using cast operator.

 int x = 0;
 double result;
 result = acos(double(x));

Also, two functions acosf() and acosl() were introduced in C99 to work specifically with type float and long double respectively.

float acosf(float x);
long double acosl(long double x);

acos() Parameter

The acos() function takes a single argument in the range of [-1, +1]. It's because the value of cosine is in the range of 1 and -1.

Parameter Description
double value Required. A double value between - 1 and +1 inclusive.

acos() Return Value

The acos() functions returns the value in range of [0.0, π] in radians. If the parameter passed to the acos() function is less than -1 or greater than 1, the function returns NaN (not a number).

Parameter (x) Return Value
x = [-1, +1] [0, π] in radians
 -1 > x or x > 1 NaN (not a number)

Example 1: acos() function with different parameters

#include <stdio.h>
#include <math.h>

int main()
{
    // constant PI is defined
    const double PI =  3.1415926;
    double x, result;

    x =  -0.5;
    result = acos(x);
    printf("Inverse of cos(%.2f) = %.2lf in radians\n", x, result);

    // converting radians to degree
    result = acos(x)*180/PI;
    printf("Inverse of cos(%.2f) = %.2lf in degrees\n", x, result);

    // paramter not in range
    x = 1.2;
    result = acos(x);
    printf("Inverse of cos(%.2f) = %.2lf", x, result);

    return 0;
}

Output

Inverse of cos(-0.50) = 2.09 in radians
Inverse of cos(-0.50) = 120.00 in degrees
Inverse of cos(1.20) = nan

Example 2: acosf() and acosl() function

#include <stdio.h>
#include <math.h>
int main()
{
    float fx, facosx;
    long double lx, ldacosx;

    // arc cosine of type float
    fx = -0.505405;
    facosx = acosf(fx);

    // arc cosine of type long double
    lx = -0.50540593;
    ldacosx = acosf(lx);

    printf("acosf(x) = %f in radians\n", facosx);
    printf("acosl(x) = %Lf in radians", ldacosx);

    return 0;
}

Output

acosf(x) = 2.100648 in radians
acosl(x) = 2.100649 in radians
Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community