# acos() function in C++ STL

The acos() is an inbuilt function in C++ STL which returns the inverse cosine of a number (argument) in radians. The value which is returned by the acos() function always lies between – to + Syntax :

```acos(data_type x)
```

Parameters :This function accepts one mandatory parameter x which specifies the value whose inverse cosine should be computed. It must lie between -1 and +1, else a domain-error is thrown. The parameter can be of double, float or long double datatype.

Return :The function returns a numeric value between – and + . It is the counterclockwise angle which is measured in radian.

Program 1:

 `// C++ program to demonstrate ` `// the acos() function ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``double` `x = 1.0; ` ` `  `    ``// Function call to calculate acos(x) value ` `    ``double` `result = ``acos``(x); ` ` `  `    ``cout << ``"acos(1.0) = "` `<< result << ``" radians"` `<< endl; ` `    ``cout << ``"acos(1.0) = "` `<< result * 180 / 3.141592 ` `         ``<< ``" degrees"` `<< endl; ` ` `  `    ``return` `0; ` `} `

Output:

```acos(1.0) = 0 radians
acos(1.0) = 0 degrees
```

Program 2:

 `// C++ program to demonstrate ` `// the acos() function ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``double` `result; ` `    ``int` `x = -1; ` ` `  `    ``// Function call to calculate acos(x) value ` `    ``result = ``acos``(x); ` ` `  `    ``cout << ``"acos(-1) = "` `<< result ` `         ``<< ``" radians"` `<< endl; ` `    ``cout << ``"acos(-1) = "` `<< result * 180 / 3.141592 ` `         ``<< ``" degrees"` `<< endl; ` ` `  `    ``return` `0; ` `} `

Output:

```acos(-1) = 3.14159 radians
acos(-1) = 180 degrees
```

Errors and Exceptions:

• The function returns no matching function for call to error when a string or character is passed as an argument.
• The function returns nan when a out of range number is passed as an argument.

Below programs illustrate the errors and exceptions of the above method:

Program 3:

 `// C++ program to demonstrate the acos() ` `// function errors and exceptions ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``double` `result; ` `    ``string x = ``"gfg"``; ` `    ``result = ``acos``(x); ` ` `  `    ``cout << ``"acos(x) = "` `<< result ` `         ``<< ``" radians"` `<< endl; ` `    ``cout << ``"acos(x) = "` `<< result * 180 / 3.141592 ` `         ``<< ``" degrees"` `<< endl; ` ` `  `    ``return` `0; ` `} `

Output:

```prog.cpp:10:17: error: no matching function for call to 'acos(std::__cxx11::string&)'
result = acos(x);
```

Program 4:

 `// C++ program to demonstrate the ` `// acos() function errors and exceptions ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``double` `x = 3.7, result; ` ` `  `    ``// Function call to calculate acos(x) value ` `    ``result = ``acos``(x); ` ` `  `    ``cout << ``"acos(3.7) = "` `<< result ` `         ``<< ``" radians"` `<< endl; ` `    ``cout << ``"acos(3.7) = "` `<< result * 180 / 3.141592 ` `         ``<< ``" degrees"` `<< endl; ` ` `  `    ``return` `0; ` `} `

Output:

```acos(3.7) = nan radians
acos(3.7) = nan degrees
```

## tags:

C++ CPP-Functions STL STL CPP