C++ std::tanh() Function

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

Definition

In C++, std::tanh(x) is the hyperbolic tangent. It equals sinh(x) / cosh(x). Its graph is an S-shaped curve that passes through 0 and flattens out at -1 on the left and 1 on the right.

This "squashing" makes it very useful. Whatever number you give it, the answer stays between -1 and 1. That is why it is popular as an activation function in neural networks. Unlike sinh() and cosh(), it never overflows.

std::tanh() 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::tanh. Start with these lines.

C++
double a = std::tanh(0.5);    // double version
float  b = std::tanh(0.5f);   // float version
CallWhat it does
std::tanh(0.5)Passes a double and returns a double. Here the result is 0.462117.
std::tanh(0.5f)Passes a float, so the float overload runs and returns a float.
std::tanh(1)An integer is converted to double. Returns 0.761594.

Write std::tanh so the C++ overloads for float, double, and long double are used.

Library declarations
C++
float       tanh(float x);
double      tanh(double x);
long double tanh(long double x);
double      tanh(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 tangentA value from -1 to 1, with the same sign as x.
1 / -1For large positive or negative x, and for inf and -inf.
0tanh(0) is exactly 0.

C++ Examples

Example 1 — Hyperbolic Tangent of Common Values

Print tanh() for a few numbers, from zero to a bigger input.

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

int main() {
    double values[] = {0.0, 0.5, 1.0, 3.0};

    for (double v : values) {
        std::cout << "tanh(" << v << ") = " << std::tanh(v) << '\n';
    }
    return 0;
}

How It Works

  1. The loop gives each number in the array to std::tanh().
  2. tanh(0) is 0, the middle of the S curve. tanh(0.5) is 0.462117, a little less than the input.
  3. The values grow more slowly as x gets bigger. tanh(1) is 0.761594, and tanh(3) is already 0.995055, very close to 1.

Example 2 — Saturation: Big Inputs All Look the Same

Feed larger and larger numbers into tanh() and watch the answer stop at 1.

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

int main() {
    for (double x : {5.0, 10.0, 100.0}) {
        std::cout << "tanh(" << x << ") = " << std::tanh(x) << '\n';
    }
    std::cout << "tanh(100) == 1: " << (std::tanh(100.0) == 1.0) << '\n';
    return 0;
}

How It Works

  1. At 5 the result is 0.999909, already within one in ten thousand of 1.
  2. At 10 the result rounds to 1 when printed with six digits. The function is flat from there on.
  3. At 100 the answer is so close to 1 that a double cannot tell the difference, so the test == 1.0 prints 1. The input is lost: this is saturation.

Example 3 — A Simple Neuron

A neuron in a neural network multiplies the input by a weight, adds a bias, and squashes the sum with tanh().

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

int main() {
    double weight = 1.5;
    double bias = 0.1;

    for (double input : {-2.0, -0.5, 0.0, 2.0}) {
        double output = std::tanh(weight * input + bias);
        std::cout << "input " << input << " -> output " << output << '\n';
    }
    return 0;
}

How It Works

  1. weight * input + bias is a straight line, so a big input can give a big sum. The line goes through tanh() next.
  2. The input 0 gives tanh(0.1), which is 0.099668. A negative input gives a negative output, like -0.993927 for -2.
  3. The input 2 gives tanh(3.1), or 0.995873. Whatever the input is, the output stays between -1 and 1.

Frequently Asked Questions

std::tanh(x) returns the hyperbolic tangent of x, which is sinh(x) / cosh(x). The result is always between -1 and 1.
The hyperbolic tangent as float, double, or long double, a value from -1 to 1. tanh(0) is 0, and tanh of a large number is 1.
The result is always between -1 and 1. tanh(0) is 0, and the curve gets closer to 1 and -1 for large numbers but never goes beyond them.
It squashes any number into the range -1 to 1, and it is centered around zero. It is smooth, so it works well when training a network.
Include <cmath> and write std::tanh(). That always works and picks the C++ overloads. The global name ::tanh often works too, but the standard does not promise it, and it may only take a double.

Next: tgamma()

Keep going through the <cmath> functions. The gamma function: a smooth factorial for decimal numbers.

tgamma() 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.

11 people found this page helpful