Function atan2() in Perl

In the Perl programming language has a built-in function atan2(). The main purpose of the function is the conversion from Cartesian coordinate system to polar.

Here's an example:

▶ Run
#!/usr/bin/perl

print atan2(321, 100);

This program displays 1.26879861219712.

Arguments

my $value = atan2($y, $x);

Arguments to Perl function atan2() — two numbers.

If the functions atan2() do not pass any argument, or pass exactly one argument, it will error:

Not enough arguments for atan2 at script.pl line 3, near "atan2()"
Execution of script.pl aborted due to compilation errors.

The function uses the default variable $_. If there is a need to use it, then it must be explicitly passed to the function atan2($_, 1);

If the functions atan2() can pass more than two arguments, it will error:

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

Return value

Function atan2() always returns a number. atan2($y, $x) returns the arctangent from $y/$x. The range of possible values is , to π, i.e., from -3.14159265358979 to 3.14159265358979.

Official documentation

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

    atan2 Y,X
            Returns the arctangent of Y/X in the range -PI to PI.

            For the tangent operation, you may use the "Math::Trig::tan"
            function, or use the familiar relation:

                sub tan { sin($_[0]) / cos($_[0])  }

            The return value for "atan2(0,0)" is implementation-defined;
            consult your atan2(3) manpage for more information.

            Portability issues: "atan2" in perlport.

Related topics

Other articles