PHP atan2() Function


Definition and Usage

The atan2() function calculates arc tan of two variables

atan2(y,x) returns the arc tangent of the two numbers x and y. although it is similar to atan(y)/atan(x), the signs of both x and y are used to determine the quadrant of the result. Accordingly for values of x and y atan2() is

atan(y/x) if x>0

atan(y/x)+pi if x>0

atan(y/x)-pi if x<0 and y<0

pi/2 if x=0 and y>0

-pi/2 if x=0 and y<0

0 if x=0 and y=0

This function returns angle in radians which is a float value.

Syntax

atan2 ( float $y , float $x ) : float

Parameters

Sr.No
Parameter & Description
1
y
dividend
2
x
divisor

Return Values

PHP atan2() function returns angle in radian which is a float number.

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 atan2(1,2) −

<?php
   $y=1;
   $x=2;
   $val=atan2($y, $x);;
   echo "atan2(" . $y ."," . $x .") = " . $val;
?>

Output

This will produce following result −

atan2(1,2) = 0.46364760900081

Example

 Live Demo

Following example calculate atan2(5, -5) −

<?php
   $y=5;
   $x=-5;
   $val=atan2($y, $x);;
   echo "atan2(" . $y ."," . $x .") = " . $val;
?>

Output

This will produce following result −

atan2(5,-5) = 2.3561944901923

Example

 Live Demo

Following program computes atan2(5,0) and returns 1.570796326795 (M_PI_2) −

<?php
   $y=5;
   $x=0;
   $val=atan2($y, $x);;
   echo "atan2(" . $y ."," . $x .") = " . $val;
?>

Output

This will produce following result −

atan2(5,0) = 1.5707963267949

Example

 Live Demo

Following example computes atan2(0,0) and returns 0

<?php
   $y=0;
   $x=0;
   $val=atan2($y, $x);;
   echo "atan2(" . $y ."," . $x .") = " . $val;
?>

Output

This will produce following result −

atan2(0,0) = 0

Updated on: 29-Jun-2020

151 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements