tanh

Returns the hyperbolic tangent of a number, an S-shaped value strictly between -1 and 1.

Signature
tanh(float $num): float
  • Added in PHP 4.1.0

Parameters

ParameterTypeDescription
$numfloatThe argument to process. Not an angle.

Return value

floatThe hyperbolic tangent of num, strictly between -1 and 1.

Errors

Throws a TypeError if num is not a number and cannot be converted to a float. Saturates to exactly 1.0 or -1.0 for large magnitudes rather than throwing.

tanh() is the hyperbolic tangent, defined as sinh($x) / cosh($x). It is the most immediately useful of the hyperbolic functions because of its shape: a smooth S-curve that passes through the origin, is symmetric about it, and asymptotically approaches -1 and 1 without ever exceeding them.

Use it as a squashing function

That bounded, monotonic shape makes tanh() the standard way to map any real number onto a fixed range. Neural network layers use it as an activation. Audio code uses it as a soft clipper, because it compresses loud peaks gradually instead of chopping them flat. Scoring systems use it to cap an unbounded metric — tanh($score / $scale) gives diminishing returns rather than letting one outlier dominate.

It saturates

Around an input of 19 the result becomes exactly 1.0 as far as a double can tell. Beyond that all information is lost, and atanh() cannot recover the original value — atanh(1.0) returns INF. Scale your input so the interesting range sits roughly between -3 and 3, where the curve actually varies.

Note it takes a plain number, not an angle: deg2rad() has no place here.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Squash an unbounded score into a -1 to 1 range with diminishing returns
  • Soft-clip an audio sample so loud peaks compress instead of clipping harshly
  • Use as the activation function in a hand-rolled neural network layer
  • Normalise a sensor reading into a bounded control signal

Frequently asked questions

What is the difference between tan() and tanh()?

tan() is the circular tangent of an angle in radians; it is periodic and unbounded, exploding near 90 degrees. tanh() is the hyperbolic tangent of a plain number; it is a smooth S-curve strictly bounded between -1 and 1.

Why does tanh() return exactly 1?

For inputs above roughly 19 the true value is closer to 1 than a double can represent, so it rounds to exactly 1.0. Scale your input down so the values you care about fall between about -3 and 3.

How is tanh() different from a sigmoid?

They are the same curve, rescaled: sigmoid(x) equals (1 + tanh(x / 2)) / 2. tanh() outputs -1 to 1 and is centred on zero, while a logistic sigmoid outputs 0 to 1 and is centred on 0.5.

More examples

The S-curve, plotted

The plot shows the characteristic shape: steep and nearly linear near zero, flattening out towards -1 and 1 at the extremes. Most of the useful variation happens between about -2 and 2.

Example · php
<?php
for ($x = -3.0; $x <= 3.0; $x += 0.5) {
    $y = tanh($x);
    $col = (int) round(($y + 1) * 20);
    printf("%5.1f %7.4f %s*\n", $x, $y, str_repeat(' ', $col));
}

Squashing an unbounded score

Dividing by a scale factor puts the interesting range inside tanh()'s active region. A raw score of 200 and one of 5000 both map close to 1, which is exactly the diminishing-returns behaviour you want when one outlier should not dominate a ranking.

Example · php
<?php
$scale = 50.0;
foreach ([0, 10, 50, 200, 5000] as $raw) {
    printf("raw %5d -> %.4f\n", $raw, tanh($raw / $scale));
}

Saturation destroys information

atanh() recovers the input perfectly for small values, but once tanh() returns exactly 1.0 the original magnitude is gone and the inverse gives INF. That is the practical limit of the function's usable domain.

Example · php
<?php
foreach ([1.0, 5.0, 19.0, 25.0] as $x) {
    $y = tanh($x);
    printf("tanh(%4.1f) = %.17f  back: %s\n",
        $x, $y, $y >= 1.0 ? 'INF' : (string) round(atanh($y), 6));
}

Discussion

  • Be the first to comment on this lesson.