Perl Tutorial Perl References

Perl Math - cosh() Function



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

cosh

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 infinite, then the result is positive infinity.
  • If the argument is zero, then the result is 1.

Syntax

cosh(x)

Parameters

x Specify the value.

Return Value

Returns the hyperbolic cosine of a value.

Example:

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

use Math::Trig;

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

The output of the above code will be:

cosh(-2) = 3.76219569108363
cosh(-1) = 1.54308063481524
cosh(0) = 1
cosh(1) = 1.54308063481524
cosh(2) = 3.76219569108363
cosh(Inf) = Inf
cosh(-Inf) = Inf
cosh(NaN) = NaN

This function can also be used to calculate complex hyperbolic cosine 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 2𝜋i.

Mathematically, it can be expressed as:

complex cosh

Example:

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

use Math::Complex;

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

The output of the above code will be:

cosh(2+2i) = -1.56562583531574+3.29789483631124i
cosh(2) = 3.76219569108363
cosh(2i) = -0.416146836547142

❮ Perl Math Functions