In C#, Math.Atanh(d) is the inverse hyperbolic tangent. It undoes Tanh(): if Tanh(x) is d, then Atanh(d) is x. Math.Atanh(0) is 0 and Math.Atanh(0.5) is about 0.5493.
Since Tanh only produces numbers strictly between -1 and 1, the inverse only accepts inputs in that range. As the input gets closer to 1 the answer shoots up, and at exactly 1 it reaches Infinity (and -Infinity at -1). Anything outside the range gives NaN. It equals 0.5 * Log((1 + x) / (1 - x)).
This is a static member of the static class System.Math. You call it with the class name, such as Math.Atanh(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Math.Atanh was added in .NET Core 3.0. It is not in classic .NET Framework.
Syntax
Pass a number from -1 to 1. The answer is a double.
C#
double a = Math.Atanh(0); // 0
double b = Math.Atanh(0.5); // 0.5493061443340548
double c = Math.Atanh(1); // Infinity
Call
What it does
Math.Atanh(0)
Returns 0.
Math.Atanh(0.5)
Returns about 0.5493.
Math.Atanh(1)
Returns Infinity. At -1 it returns -Infinity.
Math.Atanh(1.5)
Returns NaN. The input is outside -1 to 1.
The legal input range is -1 to 1. The two edges give Infinity and -Infinity, and everything beyond them gives NaN, with no exception. Atanh is an odd function: Atanh(-x) equals -Atanh(x). Round non-whole answers before printing.
Library declarations
C#
public static double Atanh(double d);
Parameters
Parameter
Description
d
Required. A double from -1 to 1.
Return Value
Return Value
Description
double
The number x such that Tanh(x) equals d. Infinity for 1, -Infinity for -1, NaN outside -1 to 1 or for NaN.
C# Examples
Example 1 — Atanh of a Few Numbers
Print Atanh for 0, 0.5, -0.5, and 0.9, rounded to 4 decimals.
C#
using System;
using System.Globalization;
class Program {
static string R(double value) {
return Math.Round(value, 4).ToString(CultureInfo.InvariantCulture);
}
static void Main() {
Console.WriteLine(R(Math.Atanh(0)));
Console.WriteLine(R(Math.Atanh(0.5)));
Console.WriteLine(R(Math.Atanh(-0.5)));
Console.WriteLine(R(Math.Atanh(0.9)));
}
}
Output
0
0.5493
-0.5493
1.4722
How It Works
Zero.Tanh(0) is 0, so Atanh(0) is 0.
Half. You need Tanh of about 0.5493 to reach 0.5, so the answer is 0.5493.
Minus half. The answer is -0.5493. The sign follows the input.
Close to the edge. At 0.9 the answer has already grown to about 1.4722, and it keeps growing as the input nears 1.
Example 2 — Atanh Reverses Tanh
Apply Tanh and then Atanh, and see the starting number come back.
C#
using System;
using System.Globalization;
class Program {
static string R(double value) {
return Math.Round(value, 6).ToString(CultureInfo.InvariantCulture);
}
static void Main() {
foreach (double x in new double[] { 0.5, 1, -2 }) {
double squashed = Math.Tanh(x);
Console.WriteLine(R(Math.Atanh(squashed)));
}
}
}
Output
0.5
1
-2
How It Works
Squash with Tanh.Tanh(x) always lands inside -1 to 1, which is exactly the input range Atanh accepts.
Unsquash with Atanh.Math.Atanh(squashed) gives back the starting number, apart from a tiny rounding error.
Rounding. The helper rounds to 6 places so the tiny error stays hidden.
Negative start. Starting from -2 gives -2 again, since Atanh keeps the sign. The squashed value, about -0.964, is still safely inside the range.
Example 3 — The Edges: Infinity and NaN
See the infinite answers at -1 and 1, NaN outside, and the Log formula.
C#
using System;
using System.Globalization;
class Program {
static void Main() {
Console.WriteLine(Math.Atanh(1));
Console.WriteLine(Math.Atanh(-1));
Console.WriteLine(Math.Atanh(1.5));
double x = 0.5;
double byFormula = 0.5 * Math.Log((1 + x) / (1 - x));
Console.WriteLine(Math.Round(byFormula, 4).ToString(CultureInfo.InvariantCulture));
}
}
Output
Infinity
-Infinity
NaN
0.5493
How It Works
The upper edge.Tanh never quite reaches 1, so to get 1 you would need an infinitely large input. Math.Atanh(1) returns Infinity.
The lower edge. The same idea at -1 gives -Infinity.
Outside the range.Tanh cannot produce 1.5, so Math.Atanh(1.5) returns NaN.
The formula.0.5 * Log((1 + x) / (1 - x)) is the textbook definition. For x = 0.5 it gives about 0.5493, the same as the method.
Frequently Asked Questions
It returns the inverse hyperbolic tangent for inputs from -1 to 1. Math.Atanh(0.5) is about 0.5493. It undoes Math.Tanh.
It returns Infinity. Math.Atanh(-1) returns -Infinity. Values outside -1 to 1 return NaN. None of these throw an exception.
It was added in .NET Core 3.0, so it is in modern .NET. It is not in classic .NET Framework, where you can use 0.5 * Math.Log((1 + x) / (1 - x)).
The input is below -1, above 1, or NaN. Tanh only produces values between -1 and 1, so no answer exists for numbers outside.
No. Math is a static class, so you call Math.Atanh(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.