Perl Tutorial Perl References

Perl Math - tan() Function



The Perl Math tan() function returns trigonometric tangent of an angle (angle should be in radians). In special cases it returns the following:

  • If the argument is NaN or an infinity, then the result is NaN.

In the graph below, tan(x) vs x is plotted.

Tan Function

Syntax

tan(x)

Parameters

x Specify the angle in radian.

Return Value

Returns the trigonometric tangent of an angle.

Example:

In the example below, tan() function is used to find out the trigonometric tangent of an angle.

use Math::Trig;

print("tan(pi/6) = ".tan(pi/6)."\n");
print("tan(pi/4) = ".tan(pi/4)."\n");
print("tan(pi/3) = ".tan(pi/3)."\n");
print("tan(deg2rad(90)) = ".tan(deg2rad(90))."\n");
print("tan(deg2rad(120)) = ".tan(deg2rad(120))."\n");
print("tan(Inf) = ".tan(Inf)."\n");
print("tan(-Inf) = ".tan(-Inf)."\n");
print("tan(NaN) = ".tan(NaN)."\n");

The output of the above code will be:

tan(pi/6) = 0.577350269189626
tan(pi/4) = 1
tan(pi/3) = 1.73205080756888
tan(deg2rad(90)) = 1.63312393531954e+16
tan(deg2rad(120)) = -1.73205080756888
tan(Inf) = NaN
tan(-Inf) = NaN
tan(NaN) = NaN

This function can also be used to calculate complex 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 real component, with period 𝜋i, and has poles of the first order along the real line, at coordinates (𝜋(1/2 + n), 0). However no common floating-point representation is able to represent 𝜋/2 exactly.

Mathematically, it can be expressed as:

complex tan

Example:

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

use Math::Complex;

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

The output of the above code will be:

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

❮ Perl Math Functions