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
Call
What 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
Parameter
Description
x
Required. A number from -1 to 1. Anything else is outside the domain. An integer is converted to double.
Return Value
Return Value
Description
Inverse tanh
A number whose hyperbolic tangent is x, with the same sign as x.
inf / -inf
Returned for x = 1 and x = -1. This is a pole error.
NaN
Returned 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;
}
atanh(1) = inf
atanh(-1) = -inf
atanh(1.5) is NaN: 1
How It Works
The tangent-like curve never reaches 1, so asking for the input that gives exactly 1 returns inf.
The same happens on the other side. atanh(-1) returns -inf.
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;
}
Output
r = 0.3 -> z = 0.30952 -> back to 0.3
r = 0.6 -> z = 0.693147 -> back to 0.6
r = 0.9 -> z = 1.47222 -> back to 0.9
How It Works
Each correlation r is inside the allowed range, so atanh(r) gives a normal number z.
The value 0.6 gives z = 0.693147, which is the natural logarithm of 2. A strong correlation like 0.9 gives 1.47222.
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.