C++ std::atanh() Function

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

Definition

In C++, std::atanh(x) is the inverse hyperbolic tangent. It undoes std::tanh(). Since tanh(0.5) is about 0.462117, atanh(0.462117) gives 0.5.

The input must lie strictly between -1 and 1, because tanh() never reaches those values. At exactly 1 and -1 the answer is plus or minus infinity. Outside that range the answer is NaN.

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

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

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

Library declarations
C++
float       atanh(float x);
double      atanh(double x);
long double atanh(long double x);
double      atanh(Integer x);   // for integer types

Parameters

ParameterDescription
xRequired. A number from -1 to 1. Anything else is outside the domain. An integer is converted to double.

Return Value

Return ValueDescription
Inverse tanhA number whose hyperbolic tangent is x, with the same sign as x.
inf / -infReturned for x = 1 and x = -1. This is a pole error.
NaNReturned when the magnitude of x is bigger than 1.

C++ Examples

Example 1 — Inverse Hyperbolic Tangent of Common Values

Print atanh() for values inside the range, including a value close to the edge.

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

int main() {
    double values[] = {0.0, 0.5, -0.5, 0.99};

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

How It Works

  1. Each value is inside the open range -1 to 1, so the answers are normal numbers.
  2. atanh(0) is 0. atanh(0.5) is 0.549306, and atanh(-0.5) is the same size with a minus sign.
  3. The answer explodes near the edge. atanh(0.99) is already 2.64665, far more than the input.

Example 2 — The Edges of the Range

See what happens at 1, at -1, and beyond them.

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

int main() {
    std::cout << "atanh(1)  = " << std::atanh(1.0) << '\n';
    std::cout << "atanh(-1) = " << std::atanh(-1.0) << '\n';
    std::cout << "atanh(1.5) is NaN: " << std::isnan(std::atanh(1.5)) << '\n';
    return 0;
}

How It Works

  1. The tangent-like curve never reaches 1, so asking for the input that gives exactly 1 returns inf.
  2. The same happens on the other side. atanh(-1) returns -inf.
  3. 1.5 is beyond the edge and cannot be a hyperbolic tangent, so the result is NaN. std::isnan() prints 1.

Example 3 — Fisher Transform of a Correlation

In statistics, a correlation r between -1 and 1 is often changed with atanh(r). Then tanh() changes it back.

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

int main() {
    for (double r : {0.3, 0.6, 0.9}) {
        double z = std::atanh(r);
        std::cout << "r = " << r << " -> z = " << z
                  << " -> back to " << std::tanh(z) << '\n';
    }
    return 0;
}

How It Works

  1. Each correlation r is inside the allowed range, so atanh(r) gives a normal number z.
  2. The value 0.6 gives z = 0.693147, which is the natural logarithm of 2. A strong correlation like 0.9 gives 1.47222.
  3. std::tanh(z) undoes the change and returns the original r each time. The two functions are exact opposites.

Frequently Asked Questions

std::atanh(x) returns the inverse hyperbolic tangent of x. The input must be between -1 and 1. At exactly 1 or -1 the result is infinity.
The inverse hyperbolic tangent as float, double, or long double. atanh(1) is inf, atanh(-1) is -inf, and a number outside -1 to 1 gives NaN.
It returns inf, and -1 returns -inf. The curve of tanh() never reaches 1, so the input that would give 1 is infinitely far away.
atanh(x) = 0.5 * log((1 + x) / (1 - x)). The library function is safer for tiny values and values near 1.
Include <cmath> and write std::atanh(). That always works and picks the C++ overloads. The global name ::atanh often works too, but the standard does not promise it, and it may only take a double.

Next: cbrt()

Keep going through the <cmath> functions. Cube root of a number.

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

14 people found this page helpful