Trusted answers to developer questions

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.

This

`cos()`

function only works for right-angled triangles.

```
cos(num)
```

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)
```

`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.

#this header for deg2rad() function use Math::Trig; #Positive number in radians $a = cos(2.3); print "cos(2.3): $a \n"; #negative number in radians $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

Related Courses