In C++, std::acosh(x) is the inverse hyperbolic cosine. It undoes std::cosh(). Since cosh(0) is 1, acosh(1) is 0. Since cosh(2) is about 3.7622, acosh(3.7622) gives 2.
The function only works for inputs of 1 or more, because cosh() never goes below 1. It returns the non-negative answer. The negative answer, -acosh(x), also has the same cosh, so you add the sign yourself if you need it.
std::acosh() is declared in <cmath> and is part of the group Hyperbolic functions. Hyperbolic functions are built from ex. They are not about angles, but they have the same names with an h added.
Syntax
Include <cmath> and call std::acosh. Start with these lines.
C++
double a = std::acosh(2.0); // double version
float b = std::acosh(2.0f); // float version
Call
What it does
std::acosh(2.0)
Passes a double and returns a double. Here the result is 1.31696.
std::acosh(2.0f)
Passes a float, so the float overload runs and returns a float.
std::acosh(1)
An integer is converted to double. Returns 0 as a double.
Write std::acosh so the C++ overloads for float, double, and long double are used.
Library declarations
C++
float acosh(float x);
double acosh(double x);
long double acosh(long double x);
double acosh(Integer x); // for integer types
Parameters
Parameter
Description
x
Required. A number that is 1 or more. Smaller numbers are outside the domain. An integer is converted to double.
Return Value
Return Value
Description
Inverse cosh
A non-negative number whose hyperbolic cosine is x.
NaN
Returned when x is less than 1. This is a domain error.
0 / inf
acosh(1) is 0, and acosh(inf) is inf.
C++ Examples
Example 1 — Inverse Hyperbolic Cosine of Common Values
Print acosh() for numbers of 1 and above.
C++
#include <cmath>
#include <iostream>
int main() {
double values[] = {1.0, 2.0, 10.0, 100.0};
for (double v : values) {
std::cout << "acosh(" << v << ") = " << std::acosh(v) << '\n';
}
return 0;
}
acosh(1) = 0
acosh(0.5) is NaN: 1
acosh(-2) is NaN: 1
How It Works
The edge value 1 is allowed, and the result is exactly 0.
0.5 is below the edge. No number has a hyperbolic cosine of 0.5, so the result is NaN and std::isnan() prints 1.
A negative input like -2 is also outside the domain, so the answer is NaN too. The program keeps running.
Example 3 — Where Does the Cable Reach a Height
A hanging cable has height y = a * cosh(x / a). Turn that around to find the distance x for a given height: x = a * acosh(y / a).
C++
#include <cmath>
#include <iostream>
int main() {
double a = 2.0;
for (double y : {2.0, 5.0, 10.0}) {
double x = a * std::acosh(y / a);
std::cout << "Height " << y << ": x = " << x << '\n';
}
return 0;
}
Output
Height 2: x = 0
Height 5: x = 3.1336
Height 10: x = 4.58486
How It Works
y / a turns the height into a number that is at least 1, which is what acosh() needs. The lowest point of the cable has y = a.
At height 2 the ratio is 1, and acosh(1) is 0. So x is 0, the bottom of the curve.
A height of 5 gives x = 3.1336, and a height of 10 gives x = 4.58486. A cable height below a would give NaN.
Frequently Asked Questions
std::acosh(x) returns the inverse hyperbolic cosine of x. The input must be 1 or more, and the result is 0 or more.
The non-negative inverse hyperbolic cosine as float, double, or long double. If x is less than 1, the result is NaN.
Numbers that are 1 or more. Values below 1 give NaN, because the hyperbolic cosine is never smaller than 1.
cosh() gives the same answer for x and -x, so the inverse has two answers. acosh() returns the non-negative one. Use -acosh(x) for the negative one.
Include <cmath> and write std::acosh(). That always works and picks the C++ overloads. The global name ::acosh often works too, but the standard does not promise it, and it may only take a double.