How to use acos() in C++

In trigonometry, the cosine function of a right-angled triangle is the ratio between the adjacent and hypotenuse sides of the triangle.

The cos1cos^{-1} function is the inverse cosine of a value, as shown below:

Figure 1 - Cos and acos functions

The acos function in C++ works exactly like the cos1cos^{-1} function in trigonometry and returns the inverse cosine of a value in radians.

To use the acos function, the cmath library needs to be included in the program, as shown below:

#include <cmath>

Parameters

The acos function takes a single mandatory parameter, i.e., an integer or floating point number in the range [-1, 1].

Return value

Given that the argument is in the required range of [-1,1], the acos function returns a value between 00 and ππ.

If the argument is greater than 11 or less than 1-1, then the acos function returns NaNNaN, i.e., not a number.

acos can have the following three return types, depending on the the provided argument:

  • float
  • double
  • long double

The figure below shows the return types in correspondence with different argument types that are passed to the acos function:

Figure 2 - Return Types

Example

The code below shows how the acos function works in C++:

#include <iostream>
#include <cmath>
using namespace std;
int main() {
// the value of x is in radians
double x = 0.5;
// the result is also in radians
cout << "acos(0.5) = " << acos(x) << endl;
return 0;
}

Free Resources

HowDev By Educative. Copyright ©2025 Educative, Inc. All rights reserved