ATAN2

ATAN2 is a mathematical SQL function that returns the arctangent of the two arguments/numbers passed. This function calculates the angle (in radians) between the positive x-axis and the point given by the coordinates (number1, number2) on it. It takes two arguments, where the first argument is the Y-coordinate and the second is the X-coordinate. If both arguments are zero, ATAN2 returns null.

ATAN2(Y,X)

  • y: The Y-coordinate of a point in the two-dimensional plane. The ATAN2 function uses this value to calculate the arctangent of the ratio Y/X.
  • x: The X-coordinate of a point in the two-dimensional plane. This value is the divider in the ratio Y/X used by ATAN2 to calculate the arctangent.

Example

SELECT ATAN2(1,0) as Atan;

Output

+---------------------+
| Atan |
+---------------------+
| 1.5707963267948966 |
+---------------------+

Explanation

The ATAN2 function in MySQL takes two arguments and returns the arc tangent of the two variables. The ATAN2(1,0) returns 1.5707963267948966 which is π/2 in radians. This example demonstrates the usage of ATAN2 function in MySQL to calculate the arc tangent value.

ATAN2(Y float, X float) → float

  • y float: This is a numerical argument representing the y-coordinate on a two-dimensional Cartesian plane. It can be any real floating-point number.
  • x float: This is a numerical argument representing the x-coordinate on a two-dimensional Cartesian plane. It returns a two-argument inverse tangent. If X == 0 and Y > 0, ATAN2 returns pi / 2; if X == 0 and Y < 0, ATAN2 returns -pi / 2. If X > 0, ATAN2 returns ATAN(Y/X). If X < 0 and Y >= 0, ATAN2 returns ATAN(Y/X) + pi. Finally, if X < 0 and Y < 0, ATAN2 returns ATAN(Y/X) - pi.
  • float: This is the returned data type of the SQL ATAN2 function. It is a floating-point number that demonstrates the angle in radians between the positive X-axis and the point (X, Y).

Example

SELECT ATAN2(1, 1) AS Atan2_Value;

Output

atan2_value
-------------
0.7853981634

Explanation

In the given example, the ATAN2() function calculates the angle (in radians) between the positive x-axis and the point given by the coordinates (1,1). The output is 0.7853981634, which is approximately the value of π/4 (45° in degrees), as expected.

ATAN2( float_expression1 , float_expression2 )

  • float_expression1: This parameter represents the Y-coordinate of a point in a two-dimensional plane. It is the numerator part of the fraction for which the arc tangent is being calculated. The value of this parameter must be any valid float type in SQL Server.
  • float_expression2: This parameter is the X-coordinate of a point on a two-dimensional plane. It acts as the denominator part of the fraction for the arc tangent calculation. This value must also be a valid float type in SQL Server. If this argument is zero, SQL Server will return a result of +/- PI/2, depending on the sign of float_expression1. If both arguments are zero, SQL Server will return the result as NULL.

Example

DECLARE @x FLOAT = 1;
DECLARE @y FLOAT = 1;
SELECT ATAN2(@y,@x) AS Result;

Output

Result
-----------
0.785398163397448

Explanation

In the example provided, ATAN2 function in SQL Server has been used to calculate the arc tangent of two values @y and @x. The ATAN2 function takes two arguments – Y and X, in that order – and returns the arctangent of Y/X, the result being expressed in radians. Here, with both @y and @x values set to 1, the output was approximately 0.785, denoting the angle in radians.

ATAN2(y NUMBER, x NUMBER) RETURN NUMBER;

  • y number: This parameter represents the numerator in a division operation used to calculate the tangent value.
  • x number: This parameter serves as the denominator in a division operation that helps compute the tangent value.
  • return number: This indicates the function’s output as a numerical value, which represents the angle in radians between the positive X-axis and the point (x, y) on the plane.

Example

SELECT ATAN2(1,1) "ATAN2" FROM dual;

Output

ATAN2
-----------
0.785398163397448

Explanation

The ATAN2 function returns the arc tangent of n and m, it determines the angle whose tangent is n/m. In this example, it returns the arc tangent of 1/1.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.