C++ std::cosh() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

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

ParameterDescription
xRequired. Any number. It is not an angle. An integer is converted to double.

Return Value

Return ValueDescription
Hyperbolic cosineThe value of (ex + e-x) / 2, always 1 or more.
infFor a very large x in either direction the result overflows to inf.
1cosh(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;
}

How It Works

  1. The array holds four exponents, and each goes through std::cosh().
  2. cosh(0) is 1, the lowest point of the curve. cosh(1) is 1.54308.
  3. cosh(-1) prints the same 1.54308, because the curve is a mirror image on both sides. cosh(2) is 3.7622.

Example 2 — The Curve Is Symmetric

Show that cosh(-x) and cosh(x) give the same number, which is not true for sinh().

C++
#include <cmath>
#include <iostream>

int main() {
    for (double x : {0.5, 1.0, 3.0}) {
        std::cout << "cosh(" << -x << ") = " << std::cosh(-x)
                  << ", cosh(" << x << ") = " << std::cosh(x) << '\n';
    }
    return 0;
}

How It Works

  1. Each line prints the cosh of a negative number and of the matching positive number.
  2. For 0.5 both sides give 1.12763. For 1 both give 1.54308. The pairs are always equal.
  3. 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;
}

How It Works

  1. The constant a = 2 sets the shape. The lowest point of the cable is at x = 0.
  2. At x = 0 the height is 2 * cosh(0), which is just 2. This is the bottom of the curve.
  3. 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.

Next: erf()

Keep going through the <cmath> functions. The error function, used in statistics and probability.

erf() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful