Related Tags

perl
math
cosine
communitycreator

# What is cos() in Perl?

harris910

The cos() function in Perl calculates the cosine of a number in radians.

The following illustration shows the mathematical representation of the cos() function.

Mathematical representation of the cosine function

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

### Syntax

cos(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

cos() returns the cosine of a number that is sent as a parameter. The return value is in the range [-1,1].

If positive/negative infinity or NAN is passed, then NAN is returned.

### Code

#this header for deg2rad() function
use Math::Trig;

$a = cos(2.3); print "cos(2.3):$a \n";

$b = cos(-2.3); print "cos(-2.3):$b \n";

#converting the degrees angle into radians and then applying cos()
#degrees = 180
$rad = deg2rad(180);$c = cos($rad); print "cos($rad): $c \n"; #NaN values$d = cos(Inf);
print "cos(Inf): $d \n";$e = cos(-Inf);
print "cos(-Inf): $e \n";$f = cos(NaN);
print "cos(NaN): \$f \n";

RELATED TAGS

perl
math
cosine
communitycreator

CONTRIBUTOR

harris910
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time 