C# Math Tanh() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In C#, Math.Tanh(value) returns the hyperbolic tangent of a number. It equals Sinh(x) divided by Cosh(x). The answer is always between -1 and 1, and Math.Tanh(0) is 0.

Imagine a dimmer switch that eases off at each end. Near zero the curve rises smoothly. For big positive numbers it flattens out near 1, and for big negative numbers it flattens near -1. That S-shape is why Tanh is popular as an activation function in neural networks: it squashes any number into a tidy range. It has nothing to do with the angle-based Tan(), and it never blows up.

This is a static member of the static class System.Math. You call it with the class name, such as Math.Tanh(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Tanh is in every .NET version. It takes and returns double.

Syntax

Pass any number. The answer is a double from -1 to 1.

C#
double a = Math.Tanh(0);      // 0
double b = Math.Tanh(1);      // 0.7615941559557649
double c = Math.Tanh(20);     // 1 (flat at the top)
CallWhat it does
Math.Tanh(0)Returns 0.
Math.Tanh(1)Returns about 0.7616.
Math.Tanh(double.PositiveInfinity)Returns 1.
Math.Tanh(double.NegativeInfinity)Returns -1.

Tanh is an odd function: Tanh(-x) equals -Tanh(x). For inputs beyond about 20, the answer is so close to 1 that a double stores exactly 1. Unlike Sinh and Cosh, Tanh never overflows. The method never throws.

Library declarations
C#
public static double Tanh(double value);

Parameters

ParameterDescription
valueRequired. Any double. It is a plain number, not an angle.

Return Value

Return ValueDescription
doubleThe hyperbolic tangent of value, from -1 to 1. Positive infinity gives 1, negative infinity gives -1, and NaN gives NaN.

C# Examples

Example 1 — Hyperbolic Tangent of a Few Numbers

Print Tanh for 0, 1, -1, and 0.5, 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.Tanh(0)));
        Console.WriteLine(R(Math.Tanh(1)));
        Console.WriteLine(R(Math.Tanh(-1)));
        Console.WriteLine(R(Math.Tanh(0.5)));
    }
}

How It Works

  1. Zero. Sinh(0) is 0 and Cosh(0) is 1, so the tangent is 0 / 1, which is 0.
  2. One. Tanh(1) is about 0.7616. Already close to the upper limit of 1.
  3. Minus one. The answer is -0.7616, the same size with the opposite sign.
  4. Half. Tanh(0.5) is about 0.4621. For small numbers the answer is a bit below the input.

Example 2 — Squash Any Number into -1 to 1

Pass small and large inputs and see how the answer stays inside the range, as an activation function does.

C#
using System;
using System.Globalization;

class Program {
    static string R(double value) {
        return Math.Round(value, 4).ToString(CultureInfo.InvariantCulture);
    }

    static void Main() {
        double[] inputs = { -3, 0, 3, 10 };

        foreach (double input in inputs) {
            Console.WriteLine(R(Math.Tanh(input)));
        }
    }
}

How It Works

  1. A mix of inputs. The array has a negative number, zero, and two positive numbers of different sizes.
  2. -3 and 3. The answers are about -0.9951 and 0.9951. The curve is already almost flat at both ends.
  3. Zero. The middle of the S-curve is 0.
  4. 10. Ten is far into the flat part. The raw answer is 0.9999999958776927, and rounding shows 1. No input ever pushes the answer past 1.

Example 3 — Infinity, NaN, and Sinh over Cosh

See the limits at infinity, how NaN behaves, and that Tanh equals Sinh divided by Cosh.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Math.Tanh(double.PositiveInfinity));
        Console.WriteLine(Math.Tanh(double.NegativeInfinity));
        Console.WriteLine(Math.Tanh(double.NaN));

        double viaDivision = Math.Sinh(1) / Math.Cosh(1);
        Console.WriteLine(Math.Round(viaDivision, 6) == Math.Round(Math.Tanh(1), 6));
    }
}

How It Works

  1. Upper limit. As the input grows without end, Tanh approaches 1. For infinity the answer is exactly 1.
  2. Lower limit. For negative infinity the answer is -1. These two limits are the walls of the S-curve.
  3. NaN. NaN in gives NaN out. No exception is thrown.
  4. The relation. Sinh(1) / Cosh(1) is the definition of Tanh(1). After rounding to 6 places, both give 0.761594, so the comparison is True.

Frequently Asked Questions

It returns the hyperbolic tangent of a number, which is Sinh(x) / Cosh(x). The answer is always between -1 and 1. Math.Tanh(0) is 0.
It squashes any number into the range -1 to 1 with a smooth S-shaped curve. This keeps values in a tidy range, so Tanh is a common activation function in neural networks.
Tan works with angles in radians and can return very large values. Tanh works with plain numbers and always stays between -1 and 1.
No. Large inputs just give 1 (or -1 for large negative inputs). Unlike Sinh and Cosh, there is no Infinity in the answer.
No. Math is a static class, so you call Math.Tanh(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.

Next: Truncate()

Drop the decimal part of a number, always moving toward zero.

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

12 people found this page helpful