Open In App

acosh() function in C++ STL

Improve
Improve
Like Article
Like
Save
Share
Report

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




// 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) = 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()
{
    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;
}


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++





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.

 



Last Updated : 02 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads