Perl sin() function

In the Perl programming language has a built-in function sin(). This function returns the value of the trigonometric function sine of your argument.

Here's an example:

▶ Run
#!/usr/bin/perl

print sin( -3.14159265358979 / 2 );

This program will display the number -1. Sinus -π/2 is -1, this is what is displayed by the program.

Arguments

Argument Perl functions sin() is a number in radians.

If the functions sin() to pass a single argument, the function will operate on it.

If you do not pass any argument, the function will work with variable \$_. Here is an example (the program displays the number 1):

▶ Run
#!/usr/bin/perl

\$_ = 3.14159265358979 / 2;

print sin;

If the functions sin() pass more than one argument, it will error:

Too many arguments for sin at script.pl line 3, near "0)"
Execution of script.pl aborted due to compilation errors.

Return value

Function sin() always returns a number. The range of possible values is -1, to 1.

Example of returned value: 0.0998334166468282 (this value is when you use the argument 0.1).

Work with degrees

The argument sin() is a number in radians. If you need to calculate the sine of degrees, they need to be converted to radians. To convert degrees to radians use the formula rad = degree * π/180.

Here is a sample program that prints a table with degrees, radians and the sine ratio:

▶ Run
#!/usr/bin/perl

use utf8;
use strict;
use warnings;
use feature qw(say);

use Math::Trig;

for (my \$degree = 0; \$degree<=360; \$degree += 15) {
my \$rad = \$degree * pi() / 180;
}

The program uses a Perl module Math::Trig. When the connection becomes available function pi(), which returns the number π.

Here is the output of this program:

0  0.00000   0.00000
15  0.26180   0.25882
30  0.52360   0.50000
45  0.78540   0.70711
60  1.04720   0.86603
75  1.30900   0.96593
90  1.57080   1.00000
105  1.83260   0.96593
120  2.09440   0.86603
135  2.35619   0.70711
150  2.61799   0.50000
165  2.87979   0.25882
180  3.14159   0.00000
195  3.40339  -0.25882
210  3.66519  -0.50000
225  3.92699  -0.70711
240  4.18879  -0.86603
255  4.45059  -0.96593
270  4.71239  -1.00000
285  4.97419  -0.96593
300  5.23599  -0.86603
315  5.49779  -0.70711
330  5.75959  -0.50000
345  6.02139  -0.25882
360  6.28319  -0.00000

Official documentation

Here is the output of the command perldoc -f sin:

sin EXPR
sin     Returns the sine of EXPR (expressed in radians).  If EXPR is
omitted, returns sine of \$_.

For the inverse sine operation, you may use the
"Math::Trig::asin" function, or use this relation:

sub asin { atan2(\$_, sqrt(1 - \$_ * \$_)) }