Computer >> Computer tutorials >  >> Programming >> PHP

PHP acos() Function


Definition and Usage

The acos() function Returns the arc cosine or cos inverse of arg in radians. acos() is the inverse function of cos(). Therefore if cos(x)=y, acos(y)=x.

For example, cos(pi/2)=0 and acos(0)=1.57079633 rad which is equal to pi/2.

This function returns a float value.

Syntax

acos ( float $arg ) : float

Parameters

Sr.NoParameter & Description
1arg
A floating point number whose arc cosine is to be calculated. Number should be withing -1 to 1

Return Values

PHP acos() function returns arc cosine of given number. It is angle represented in radians.

PHP Version

This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.

Example

Following example calculates acos(0.5) and returns 1.04719755 radians which is equivalent to pi/3 −

<?php
   $arg=0.5;
   $val=acos($arg);
   echo "acos(" . $arg . ") = " . $val . " radians";
?>

Output

This will produce the following result −

acos(0.5) = 1.0471975511966 radians

Example

Following example calculates acos(0) and returns 1.5707963267949 radians which is equivalent to pi/2 −

<?php
   $arg=0;
   $val=acos($arg);
   echo "acos(" . $arg . ") = " . $val . " radians";
?>

Output

This will produce the following result −

acos(0) = 1.5707963267949 radians

Example

Let's check find out acos(-1). It returns 3.14159265 radians (which is pi) −

<?php
   $arg=-1;
   $val=acos($arg);
   echo "acos(" . $arg . ") = " . $val . " radians";
?>

Output

This will produce the following result −

acos(-1) = 3.1415926535898 radians

Example

The following example returns NAN as the argument number is not within -1 to 1.

<?php
   $arg=5;
   $val=acos($arg);
   echo "acos(" . $arg . ") = " . $val . " radians";
?>

Output

This will produce the following result −

acos(5) = NAN radians