Home > Article > Backend Development > PHP cos() function
cos()The function returns the cosine ratio of a given angle in radians. In trigonometry, the cosine of an angle is defined as the ratio of the length of the adjacent side to the hypotenuse.
cos(x) = adjacent/hypotenuse
If x =90 degrees, cos(x) = 0.
This function returns a floating point value.
<strong>cos</strong> ( float $arg ) : float
Sr.No | Parameters and description |
---|---|
1 |
arg Floating point value representing the angle in radians |
The PHP cos() function returns the cosine ratio of the given parameters.
This function is available in PHP version 4.x, PHP 5.x and PHP 7.x.
Real-time demonstration
The following example calculates cos(pi/2) and returns 6.1232339957368E-17 (very close to 0). The cosine ratio of 90 degrees is 0 −
<?php $arg=M_PI_2;//represents pi/2 i.e. 90 deg. $val=cos($arg); echo "cos(" . $arg . ") = " . $val; ?>
This will produce the following results-
cos(1.5707963267949) = 6.1232339957368E-17
Live Demonstration
The following example uses the deg2rad() function to convert degrees to radians and then uses cos(60). The result is 0.5 -
<?php $arg=deg2rad(60); $val=cos($arg); echo "cos(" . $arg . ") = " . $val; ?>
This will produce the following result-
cos(1.0471975511966) = 0.5
Live Demo
Let's check it out cos(0). It returns 1 -
<?php $arg=0; $val=cos($arg); echo "cos(" . $arg . ") = " . $val; ?>
This will produce the following result-
cos(0) = 1
Live Demonstration
The following example calculates cos (pi) and returns -1
<?php $arg=M_PI; $val=cos($arg); echo "cos(" . $arg . ") = " . $val; ?>
This will produce the following result-
cos(3.1415926535898) = -1
The above is the detailed content of PHP cos() function. For more information, please follow other related articles on the PHP Chinese website!