Java Math tanh() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 5+

Definition

In Java, Math.tanh(x) returns the hyperbolic tangent of x, which is sinh(x) / cosh(x). It squeezes every number into the range -1.0 to 1.0: small values stay close to x, and large values flatten out near 1 or -1. Math.tanh(0) is 0.0.

That S-shaped curve is why tanh is a popular activation function in neural networks, and a handy way to limit a value smoothly instead of cutting it off. It is odd (tanh(-x) = -tanh(x)). It was added in Java 5.

A static method of the java.lang.Math class, added in Java 5 (JDK 1.5). Call it on the class name, as in Math.tanh(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass any double. The result is between -1.0 and 1.0.

Java
double a = Math.tanh(0.0);                  // 0.0
double b = Math.tanh(0.5);                  // about 0.4621
double c = Math.tanh(20.0);                 // 1.0
double out = Math.tanh(weightedSum);        // neuron output
CallWhat it does
Math.tanh(0.0)Returns 0.0.
Math.tanh(0.5)About 0.4621.
Math.tanh(Double.POSITIVE_INFINITY)Returns 1.0.
Math.tanh(Double.NEGATIVE_INFINITY)Returns -1.0.

There is only a double version and no restricted domain. For very large inputs the true result is so close to 1 that the nearest double is exactly 1.0 (this already happens at 20), so the output saturates.

Library declarations
Java
// java.lang.Math
public static double tanh(double x)

Parameters

ParameterDescription
xThe number whose hyperbolic tangent is wanted. Not an angle.

Return Value

Return ValueDescription
doubleThe hyperbolic tangent of x, from -1.0 to 1.0. The result is NaN if x is NaN, +1.0 or -1.0 for infinite x, and a zero with the same sign if x is zero.

Compare with Similar Methods

The hyperbolic trio and related curves:

MethodHow it differs
sinh(x)The numerator of tanh: odd and unbounded.
cosh(x)The denominator of tanh: even and at least 1.
exp(x)Needed in the textbook formula for tanh, which breaks for large x.
tan(a)The circular tangent: periodic, needs radians, unbounded.

Java Examples

Example 1 — Hyperbolic Tangent Squeezes Everything Into -1 to 1

Watch the value climb toward 1 and then stick to it.

Java
import java.util.Locale;

public class MathTanhBasics {
    public static void main(String[] args) {
        System.out.println("tanh(0.0)  = " + Math.tanh(0.0));
        System.out.println("tanh(0.5)  = " + String.format(Locale.US, "%.4f", Math.tanh(0.5)));
        System.out.println("tanh(-0.5) = " + String.format(Locale.US, "%.4f", Math.tanh(-0.5)));
        System.out.println("tanh(5.0)  = " + String.format(Locale.US, "%.4f", Math.tanh(5.0)));
        System.out.println("tanh(20.0) = " + Math.tanh(20.0));
        System.out.println("tanh(Infinity)  = " + Math.tanh(Double.POSITIVE_INFINITY));
        System.out.println("tanh(-Infinity) = " + Math.tanh(Double.NEGATIVE_INFINITY));
    }
}

How It Works

  1. Odd and centred. The curve crosses zero at 0 and is point-symmetric, so 0.5 and -0.5 give 0.4621 and -0.4621.
  2. Climbing, then flattening. At 5.0 the value is already 0.9999, within 0.0001 of the limit, so further growth in the input barely moves the result.
  3. Saturation in a double. The exact tanh(20) is 1 minus about 8E-18, which is closer to 1.0 than to the next smaller double, so Java prints exactly 1.0, as it does for infinity.

Example 2 — A Tiny Neuron: Weighted Sum, Then tanh

A neuron adds up its inputs times their weights plus a bias, then squashes the sum with tanh. Afterwards, tabulate the squashing curve itself.

Java
import java.util.Locale;

public class MathTanhNeuron {
    public static void main(String[] args) {
        double[] inputs = {0.5, -1.2, 2.0};
        double[] weights = {0.8, 0.3, -0.5};
        double sum = 0.1;
        for (int i = 0; i < inputs.length; i++) {
            sum += inputs[i] * weights[i];
        }
        System.out.println(String.format(Locale.US, "weighted sum = %.2f", sum));
        System.out.println(String.format(Locale.US, "neuron output = %.4f", Math.tanh(sum)));

        int[] samples = {-3, -1, 0, 1, 3};
        for (int x : samples) {
            System.out.println(String.format(Locale.US, "x=%2d -> %.4f", x, Math.tanh(x)));
        }
    }
}

How It Works

  1. Bias plus products. The sum starts at the bias 0.1, then adds 0.4, -0.36, and -1.0, which totals -0.86 (a negative sum because the third input has a negative weight).
  2. A bounded output. tanh(-0.86) is -0.6963: whatever the sum, the neuron's output stays between -1 and 1, so later layers receive numbers on a predictable scale.
  3. The curve itself. Inputs 3 and -3 are already near the limits (0.9951 and -0.9951), and the sample at 0 gives exactly zero, which is why this activation is described as zero-centred.

Example 3 — The Textbook Formula Breaks for Large Inputs

Compare Math.tanh with (e^(2x) - 1) / (e^(2x) + 1) at a normal and at a huge input.

Java
import java.util.Locale;

public class MathTanhFormula {
    public static void main(String[] args) {
        double[] xs = {1.0, 400.0};
        for (double x : xs) {
            double e = Math.exp(2 * x);
            double byFormula = (e - 1) / (e + 1);
            System.out.println(String.format(Locale.US, "x = %.1f: tanh = %.4f, formula = %.4f", x, Math.tanh(x), byFormula));
        }
        System.out.println("exp(2 * 400) = " + Math.exp(800.0));
    }
}

How It Works

  1. Fine for moderate x. At x = 1 the exponential is about 7.39, and the formula gives the same 0.7616 as the library method.
  2. An overflow, then Infinity over Infinity. exp(800) is larger than any double, so it becomes Infinity, and (Infinity - 1) / (Infinity + 1) is Infinity / Infinity, which is NaN.
  3. The library handles it. Math.tanh(400) does not need the huge intermediate value and simply returns 1.0, shown as 1.0000 by %.4f.

Frequently Asked Questions

It returns the hyperbolic tangent of a double, sinh(x) / cosh(x), a value between -1.0 and 1.0. Math.tanh(0) is 0.0.
From -1.0 to 1.0. The result approaches those limits for large inputs, and in a double it equals exactly 1.0 or -1.0 from about 20 upward in magnitude.
It is a smooth, zero-centred function that squashes any sum into -1 to 1, and its slope is easy to compute. That makes it a classic activation function.
No. tan is the circular tangent: it needs an angle in radians and can be arbitrarily large. tanh is the hyperbolic tangent: it takes any number and stays between -1 and 1.
Java 5 (JDK 1.5). Math.sinh and Math.cosh were added at the same time.

Next: toDegrees()

Convert an angle from radians to degrees.

toDegrees() 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