C acos()

The acos() function defined in the math.h header file. It helps to return the arc cosine value of the given number in radians. The arc cosine means inverse cosine value.


double acos(double x); #where x can be in int, float or long double

Also, two functions acosf() and acosl() were used with type float and long double respectively.


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

acos() Parameters:

The acos() function takes a single parameter between the range 1 ≥ x ≥ -1. Using cast operator we can explicitly convert the type to double to find the arc cosine of type int, float, or long double.

Parameter Description Required / Optional
double value A double value between - 1 and +1 Required

acos() Return Value

The return value of acos() function is in the range of [0.0, π] in radians.

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

Examples of acos()

Example 1: Working of acos() function in C?


#include <stdio.h>
#include <math.h>
int main()
{
    
    const double PI =  3.1415926;
    double v, output;

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

    //radians to degree convertion
    output = acos(v)*180/PI;
    printf("The Inverse of cos(%.2f) = %.2lf in degrees\n", v, output);

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

    return 0;
}

Output:


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

Example 2: How acosf() and acosl() functions worked in C?


#include <stdio.h>
#include <math.h>
int main()
{
    float f, fcosx;
    long double l, lcosx;

    // type float
    f = -0.505405;
    fcosx = acosf(f);

    //type long double
    l = -0.50540593;
    lcosx = acosl(l);

    printf("acosf(x) is equal to %f in radians\n", fcosx);
    printf("acosl(x) is equal to %Lf in radians", lcosx);

    return 0;
}

Output:


acosf(x) is equal to 2.100648 in radians
acosl(x) is equal to 2.100649 in radians