In C++, std::cosh(x) is the hyperbolic cosine. Its formula is cosh(x) = (ex + e-x) / 2. It is the shape of a chain or cable that hangs between two poles, the catenary.
The smallest value is cosh(0) = 1, and it rises on both sides of zero. That makes it an even function: cosh(-x) equals cosh(x). It is not an angle function, so you do not convert degrees to radians.
std::cosh() 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::cosh. Start with these lines.
C++
double a = std::cosh(1.0); // double version
float b = std::cosh(1.0f); // float version
Call
What it does
std::cosh(1.0)
Passes a double and returns a double. Here the result is 1.54308.
std::cosh(1.0f)
Passes a float, so the float overload runs and returns a float.
std::cosh(0)
An integer is converted to double. Returns 1 as a double.
Write std::cosh so the C++ overloads for float, double, and long double are used.
Library declarations
C++
float cosh(float x);
double cosh(double x);
long double cosh(long double x);
double cosh(Integer x); // for integer types
Parameters
Parameter
Description
x
Required. Any number. It is not an angle. An integer is converted to double.
Return Value
Return Value
Description
Hyperbolic cosine
The value of (ex + e-x) / 2, always 1 or more.
inf
For a very large x in either direction the result overflows to inf.
1
cosh(0) is exactly 1.
C++ Examples
Example 1 — Hyperbolic Cosine of Common Values
Print cosh() for a few numbers, including a negative one.
C++
#include <cmath>
#include <iostream>
int main() {
double values[] = {0.0, 1.0, -1.0, 2.0};
for (double v : values) {
std::cout << "cosh(" << v << ") = " << std::cosh(v) << '\n';
}
return 0;
}
Each line prints the cosh of a negative number and of the matching positive number.
For 0.5 both sides give 1.12763. For 1 both give 1.54308. The pairs are always equal.
The same holds for 3, where both sides give 10.0677. The value grows quickly as x moves away from zero.
Example 3 — Height of a Hanging Cable
A cable between two poles hangs as y = a * cosh(x / a). Here x is the horizontal distance from the lowest point.
C++
#include <cmath>
#include <iostream>
int main() {
double a = 2.0;
for (double x : {0.0, 2.0, 4.0}) {
double y = a * std::cosh(x / a);
std::cout << "x = " << x << ": height " << y << '\n';
}
return 0;
}
Output
x = 0: height 2
x = 2: height 3.08616
x = 4: height 7.52439
How It Works
The constant a = 2 sets the shape. The lowest point of the cable is at x = 0.
At x = 0 the height is 2 * cosh(0), which is just 2. This is the bottom of the curve.
The cable rises on the sides: 3.08616 at x = 2 and 7.52439 at x = 4. It rises slowly at first and then faster.
Frequently Asked Questions
std::cosh(x) returns the hyperbolic cosine of x, which is (e^x + e^-x) / 2. The result is never smaller than 1.
The hyperbolic cosine as float, double, or long double. The result is always 1 or more. A very large positive or negative x gives inf.
Its formula is (e^x + e^-x) / 2. The two terms are always positive, and their average is smallest when x is 0, where the value is exactly 1.
No. cos() takes an angle in radians and gives values from -1 to 1. cosh() is the hyperbolic cosine, built from e^x, with values of 1 or more.
Include <cmath> and write std::cosh(). That always works and picks the C++ overloads. The global name ::cosh often works too, but the standard does not promise it, and it may only take a double.