Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

acosh() function in C++ STL

  • Difficulty Level : Hard
  • Last Updated : 29 Nov, 2021

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: 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
acosh(data_type x)

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,

DatatypePrototype
For doubledouble acosh(double x);

Explicit conversion is required from (int, float or long double) to double,

DatatypePrototype
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));



Below programs illustrate the above approach: 

Example 1:

CPP




// C++ program to demonstrate
// the acosh() function
#include <bits/stdc++.h>
using namespace std;
 
// Driver Code
int main()
{
    double x = 50.0;
 
    // Function call to calculate acosh(x) value
    double result = acosh(x);
 
    cout << "acosh(50.0) = " << result << " radians"
         << endl;
    cout << "acosh(50.0) = " << result * 180 / 3.141592
         << " degrees" << endl;
    return 0;
}
Output
acosh(50.0) = 4.60507 radians
acosh(50.0) = 263.851 degrees

Example 2: 

CPP




// C++ program to demonstrate
// the acosh() function
#include <bits/stdc++.h>
using namespace std;
 
// Driver Code
int main()
{
    int x = 40.0;
 
    // Function call to calculate acosh(x) value
    double result = acosh(x);
 
    cout << "acosh(40.0) = " << result << " radians"
         << endl;
    cout << "acosh(40.0) = " << result * 180 / 3.141592
         << " degrees" << endl;
    return 0;
}
Output
acosh(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++




// C++ program to demonstrate
// the acosh() function
// value less than 1
#include <bits/stdc++.h>
using namespace std;
 
// Driver Code
int main()
{
    double x = -50.0;
 
    // Function call to calculate acosh(x) value
    double result = acosh(x);
 
    cout << "acosh(-50.0) = " << result << " radians"
         << endl;
    cout << "acosh(-50.0) = " << result * 180 / 3.141592
         << " degrees" << endl;
    return 0;
}
Output
acosh(-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




// C++ program to demonstrate
// the acosh() function
#include <bits/stdc++.h>
using namespace std;
 
// Driver Code
int main()
{
    string x = "gfg";
 
    // Function call to calculate acosh(x) value
    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. 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!