What is tan() in Perl?

The tan() function in Perl calculates and returns the tangent of a number in radians.

The illustration below shows the mathematical representation of the tan() function.

Mathematical representation of tangent function

This tan() function only works for right-angled triangles.

Syntax

tan(num)

Parameter

This function requires a number that represents an angle in radians as a parameter.

To convert degrees to radians, use the following function in Perl.

deg2rad(angleInDegrees)

Return value

tan() returns the tangent of a number that is set as a parameter.

  • If positive/negative infinity or NaNnot a number is passed as an argument, the value returned is NaN.

Code

#this header for deg2rad() function
use Math::Trig;
#Positive number in radians
$a = tan(2.3);
print "tan(2.3): $a \n";
#negative number in radians
$b = tan(-2.3);
print "tan(-2.3): $b \n";
#converting the degrees angle into radians and then applying tan()
#degrees = 45
$rad = deg2rad(45);
$c = tan($rad);
print "tan($rad): $c \n";
#infinite values
$d = tan(Inf);
print "tan(Inf): $d \n";
$e = tan(-Inf);
print "tan(-Inf): $e \n";
#NaN value
$f = tan(NaN);
print "tan(NaN): $f \n";

Free Resources