Perl Tutorial Perl References

Perl Math - acos() Function



The Perl Math acos() function returns arc cosine of a value. The returned value will be in the range 0 through 𝜋.

Syntax

acos(x)

Parameters

x Specify the value.

Return Value

Returns the arc cosine of the value.

Example:

In the example below, acos() function is used to find out the arc cosine of a given value.

use Math::Trig;

print("acos(-1) = ".acos(-1)."\n");
print("acos(-0.5) = ".acos(-0.5)."\n");
print("acos(0) = ".acos(0)."\n");
print("acos(0.5) = ".acos(0.5)."\n");
print("acos(1) = ".acos(1)."\n");

The output of the above code will be:

acos(-1) = 3.14159265358979
acos(-0.5) = 2.0943951023932
acos(0) = 1.5707963267949
acos(0.5) = 1.0471975511966
acos(1) = 0

This function can also be used to calculate arc cosine of a complex number z. It is a function on complex plane, and has two branch cuts:

  • Extends right from 1 along the real axis to ∞, continuous from below.
  • Extends left from -1 along the real axis to -∞, continuous from above.

Mathematically, it can be expressed as:

complex acos

Example:

In the example below, acos() function is used to find out the complex arc cosine of a given complex number.

use Math::Complex;

$z1 = 2 + 2*i;
$z2 = 2;
$z3 = 2*i;
print("acos($z1) = ".acos($z1)."\n");
print("acos($z2) = ".acos($z2)."\n");
print("acos($z3) = ".acos($z3)."\n");

The output of the above code will be:

acos(2+2i) = 0.816547182096851-1.73432452148797i
acos(2) = 1.31695789692482i
acos(2i) = 1.5707963267949-1.44363547517881i

❮ Perl Math Functions