Library contents

C++ std::cosh

In C++, std::cosh returns the hyperbolic cosine of x, equal to (eˣ + e⁻ˣ) / 2. At zero the result is 1.

A simple example

std::cosh

  1. Start with 0
  2. Do this Evaluate hyperbolic cosine
  3. Result 1
At zero the two exponential terms both equal 1. Their average is 1.
lesson.cpp
#include <iostream>
#include <cmath>

int main() {
    std::cout << (std::cosh(0.0)) << '\n';
}
Output
1

Syntax

C++ syntax
std::cosh(offset)
Run these examples in C++

Save one example at a time as lesson.cpp. Use C++17 and a Unix-style shell.

c++ -std=c++17 -Wall -Wextra lesson.cpp -o lesson
./lesson

Estimate the shape of a hanging cable

An engineering class models a freely hanging cable. Evaluate the curve at the sample position so the team can relate the plotted shape or slope to that point on the cable.

From the programconst double offset = 0.5
Result1.128
lesson.cpp
#include <cmath>
#include <cstdio>

int main() {
    const double offset = 0.5;

    std::printf("%.3f\n", std::cosh(offset));
    return 0;
}
Output
1.128

At half a scale length from the centre, the normalized height is about 1.128. Subtract one for the rise above the lowest point.

Advertisements

Plot the bottom of a cable model

A student models a cable using normalized height y = cosh(x). The drawing starts at the centre, where x is zero.

Given0
Worked result1.000

Using 0 gives 1.000 for this measurement.

Plot a point beside the centre

A scale drawing uses a cable whose normalized height follows cosh(x). The designer samples it one scale length from the centre.

Given1
Worked result1.543

Using 1 gives 1.543 for this measurement.

What to watch for

Absolute height depends on the chosen origin and cable scale.

Documentation for std::cosh