Home  >  Article  >  Backend Development  >  PHP cos() function

PHP cos() function

WBOY
WBOYforward
2023-08-26 10:29:07946browse

PHP cos() 函数

Definition and Usage

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.

Syntax

<strong>cos</strong> ( float $arg ) : float

Parameters

Sr.No Parameters and description
1 arg

Floating point value representing the angle in radians

Return value

The PHP cos() function returns the cosine ratio of the given parameters.

PHP Version

This function is available in PHP version 4.x, PHP 5.x and PHP 7.x.

Example

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;
?>

Output

This will produce the following results-

cos(1.5707963267949) = 6.1232339957368E-17

Example

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;
?>

Output

This will produce the following result-

cos(1.0471975511966) = 0.5

Example

Live Demo

Let's check it out cos(0). It returns 1 -

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

Output

This will produce the following result-

cos(0) = 1

Example

Live Demonstration

The following example calculates cos (pi) and returns -1

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

output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete