Perl Tutorial Perl References

Perl Math - tanh() Function



The Perl Math tanh() function returns hyperbolic tangent of a value. The hyperbolic tangent of x is defined as:

tanh

where e is an Euler's number.

In special cases it returns the following:

  • If the argument is NaN, then the result is NaN.
  • If the argument is positive infinity, then the result is 1.
  • If the argument is negative infinity, then the result is -1.

Syntax

tanh(x)

Parameters

x Specify the value.

Return Value

Returns the hyperbolic tangent of a value.

Example:

In the example below, tanh() function is used to find out the hyperbolic tangent of a value.

use Math::Trig;

print("tanh(-2) = ".tanh(-2)."\n");
print("tanh(-1) = ".tanh(-1)."\n");
print("tanh(0) = ".tanh(0)."\n");
print("tanh(1) = ".tanh(1)."\n");
print("tanh(2) = ".tanh(2)."\n");
print("tanh(Inf) = ".tanh(Inf)."\n");
print("tanh(-Inf) = ".tanh(-Inf)."\n");
print("tanh(NaN) = ".tanh(NaN)."\n");

The output of the above code will be:

tanh(-2) = -0.964027580075817
tanh(-1) = -0.761594155955765
tanh(0) = 0
tanh(1) = 0.761594155955765
tanh(2) = 0.964027580075817
tanh(Inf) = 1
tanh(-Inf) = -1
tanh(NaN) = NaN

This function can also be used to calculate complex hyperbolic tangent of a complex number z. It is a function on complex plane, and has no branch cuts. It is periodic with respect to the imaginary component, with period 𝜋i, and has poles of the first order along the imaginary line, at coordinates (0, 𝜋(1/2 + n)). However no common floating-point representation is able to represent 𝜋/2 exactly.

Mathematically, it can be expressed as:

complex tanh

Example:

In the example below, tanh() function is used to find out the complex hyperbolic tangent of the given number.

use Math::Complex;

$z1 = 2 + 2*i;
$z2 = 2;
$z3 = 2*i;
print("tanh($z1) = ".tanh($z1)."\n");
print("tanh($z2) = ".tanh($z2)."\n");
print("tanh($z3) = ".tanh($z3)."\n");

The output of the above code will be:

tanh(2+2i) = 1.02383559457047-0.0283929528682322i
tanh(2) = 0.964027580075817
tanh(2i) = -2.18503986326152i

❮ Perl Math Functions

5