PHP cos() Function

PHPServer Side ProgrammingProgramming

Definition and Usage

The cos() function returns the cosine ratio of given angle in radians. In trigonometry, cosine of an angle is defined as ratio of lengths of adjacent side and hypotenuse.

cos(x) = adjacent/hypotenuse

If x=90 degree, cos(x) = 0.

This function returns a float value.

Syntax

cos ( float $arg ) : float

Parameters

Sr.NoParameter & Description
1arg
A floating point value that represents angle in radians

Return Values

PHP cos() function returns cosine ratio of given parameter.

PHP Version

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

Example

 Live Demo

Following example calculates cos(pi/2) and returns 6.1232339957368E-17 (very close to 0). Cosine ratio of 90 deg is 0 −

<?php
   $arg=M_PI_2;//represents pi/2 i.e. 90 deg.
   $val=cos($arg);
   echo "cos(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

cos(1.5707963267949) = 6.1232339957368E-17

Example

 Live Demo

Following example uses deg2rad() function to convert degrees to radians and then uses cos(60). Result is 0.5 −

<?php
   $arg=deg2rad(60);
   $val=cos($arg);
   echo "cos(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

cos(1.0471975511966) = 0.5

Example

 Live Demo

Let's check find out cos(0). It returns 1 −

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

Output

This will produce following result −

cos(0) = 1

Example

 Live Demo

Following example computes cos(pi) and returns -1

<?php
   $arg=M_PI;
   $val=cos($arg);
   echo "cos(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

cos(3.1415926535898) = -1
raja
Published on 29-Jun-2020 12:07:20
Advertisements