The acosh() is an inbuilt function in C++ STL that returns the inverse hyperbolic cosine of an angle given in radians. The function belongs to <cmath> header file.
Syntax:
acosh(data_type x)
Time Complexity: O(1)
Auxiliary Space: O(1)
Parameter: The function accepts one mandatory parameter x which specifies the inverse hyperbolic angle in radian which should be greater or equal to 1. If the argument is less than 1, -nan is returned. The parameter can be of double, float, or long double datatype.
Return: The acosh() function returns the inverse hyperbolic sine of the argument in radians which is in the range [0, inf].
According to C++ 11 standard, there are various prototypes available for acosh() function,
Datatype | Prototype |
---|
For double | double acosh(double x); |
Explicit conversion is required from (int, float or long double) to double,
Datatype | Prototype |
---|
For int | int a = 0; double b = acosh(double(a)); |
For float | float a = 0; double b = acosh(double(a)); |
For long double | long double a = 0; double b = acosh(double(a)); |
The below programs illustrate the above approach:
Example 1:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
double x = 50.0;
double result = acosh(x);
cout << "acosh(50.0) = " << result << " radians"
<< endl;
cout << "acosh(50.0) = " << result * 180 / 3.141592
<< " degrees" << endl;
return 0;
}
|
Outputacosh(50.0) = 4.60507 radians
acosh(50.0) = 263.851 degrees
Example 2:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x = 40.0;
double result = acosh(x);
cout << "acosh(40.0) = " << result << " radians"
<< endl;
cout << "acosh(40.0) = " << result * 180 / 3.141592
<< " degrees" << endl;
return 0;
}
|
Outputacosh(40.0) = 4.38187 radians
acosh(40.0) = 251.063 degrees
If we pass a negative value(value less than 1), the function returns NaN (not a number).
Example 3:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
double x = -50.0;
double result = acosh(x);
cout << "acosh(-50.0) = " << result << " radians"
<< endl;
cout << "acosh(-50.0) = " << result * 180 / 3.141592
<< " degrees" << endl;
return 0;
}
|
Outputacosh(-50.0) = -nan radians
acosh(-50.0) = -nan degrees
Errors and Exceptions: The function returns no matching function for call to error when a string or a character is passed as an argument.
Example:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
string x = "gfg" ;
double result = acosh(x);
cout << "acosh(50.0) = " << result << " radians"
<< endl;
cout << "acosh(50.0) = " << result * 180 / 3.141592
<< " degrees" << endl;
return 0;
}
|
Error:
prog.cpp: In function ‘int main()’:
prog.cpp:12:28: error: no matching function for call to ‘acosh(std::__cxx11::string&)’
double result = acosh(x);
The above program generates an error if no matching function for call as a string is passed as an argument.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.