In trigonometry, the cosine
function of a right-angled triangle is the ratio between the adjacent and hypotenuse sides of the triangle.
The function is the inverse cosine of a value, as shown below:
The acos
function in C++ works exactly like the 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>
The acos
function takes a single mandatory parameter, i.e., an integer or floating point number in the range [-1, 1].
Given that the argument is in the required range of [-1,1], the acos
function returns a value between and .
If the argument is greater than or less than , then the acos
function returns , i.e., not a number.
acos
can have the following three return types, depending on the the provided argument:
The figure below shows the return types in correspondence with different argument types that are passed to the acos
function:
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 radiansdouble x = 0.5;// the result is also in radianscout << "acos(0.5) = " << acos(x) << endl;return 0;}
Free Resources