PHP cos() function

WBOY
Release: 2023-08-26 10:30:02
forward
919 people have browsed it

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
Copy after login

Parameters

Sr.NoParameters and description
1arg< /p>

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;
?>
Copy after login

Output

This will produce the following results-

cos(1.5707963267949) = 6.1232339957368E-17
Copy after login

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;
?>
Copy after login

Output

This will produce the following result-

cos(1.0471975511966) = 0.5
Copy after login

Example

Live Demo

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

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

Output

This will produce the following result-

cos(0) = 1
Copy after login

Example

Live Demonstration

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

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

output

This will produce the following result-

cos(3.1415926535898) = -1
Copy after login

The above is the detailed content of PHP cos() function. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!