# acos() function with example in C++

C++ acos() function: Here, we are going to learn about the acos() function with example of cmath header in C++ programming language?
Submitted by IncludeHelp, on April 28, 2019

## C++ acos() function

acos() function is a library function of cmath header, it is used to find the principal value of the arc cosine of the given number, it accepts a number (x) and returns the principal value of the arc cosine of x in radians.

Note: Value (x) must be between -1 to +1, else it will return a domain error (nan).

Syntax of acos() function:

```    acos(x);
```

Parameter(s): x – is the value whose arc cosine to be calculated.

Return value: double – it returns double type value that is the principal value of the arc cosine of the given number x.

Example:

```    Input:
float x = 0.65;

Function call:
acos(x);

Output:
0.863212
```

### C++ code to demonstrate the example of acos() function

```// C++ code to demonstrate the example of
// acos() function

#include <iostream>
#include <cmath>
using namespace std;

// main() section
int main()
{
float x;

x = -1.0;
cout<<"acos("<<x<<"): "<<acos(x)<<endl;

x = -0.89;
cout<<"acos("<<x<<"): "<<acos(x)<<endl;

x = 0.65;
cout<<"acos("<<x<<"): "<<acos(x)<<endl;

x = 1;
cout<<"acos("<<x<<"): "<<acos(x)<<endl;

return 0;
}
```

Output

```acos(-1): 3.14159
acos(-0.89): 2.66814
acos(0.65): 0.863212
acos(1): 0
```

Example with domain error

If we provide the value out of the range (except -1 to +1), it returns nan.

```// C++ code to demonstrate the example of
// acos() function

#include <iostream>
#include <cmath>
using namespace std;

// main() section
int main()
{
float x;

x = -0.89;  //no error
cout<<"acos("<<x<<"): "<<acos(x)<<endl;

x = 2.65;   //error
cout<<"acos("<<x<<"): "<<acos(x)<<endl;

x = -1.25;  //error
cout<<"acos("<<x<<"): "<<acos(x)<<endl;

return 0;
}
```

Output

```acos(-0.89): 2.66814
acos(2.65): nan
acos(-1.25): nan
```

Reference: C++ acos() function