MySQL ATAN2() Function – Return the Arc Tangent of 2 Values

In MySQL, the ATAN2() function returns the arc tangent of two values.

You provide the two values as comma-separated arguments when calling the function.

Syntax

The syntax goes like this:

ATAN2(Y,X)

This is similar to calculating the arc tangent of Y / X, except that the signs of both arguments are used to determine the quadrant of the result.

Example 1 – Basic Usage

Here’s a basic example to demonstrate how this function works.

SELECT ATAN2(2, 1);

Result:

+--------------------+
| ATAN2(2, 1)        |
+--------------------+
| 1.1071487177940904 |
+--------------------+

And another example using different values.

SELECT ATAN2(2.5, 1.2);

Result:

+--------------------+
| ATAN2(2.5, 1.2)    |
+--------------------+
| 1.1232763516377267 |
+--------------------+

Example 2 – Negative Values

Here’s an example using negative values.

SELECT ATAN2(-2, -1);

Result:

+---------------------+
| ATAN2(-2, -1)       |
+---------------------+
| -2.0344439357957027 |
+---------------------+

And here’s an example combining positive and negative values.

SELECT ATAN2(2, -1);

Result:

+--------------------+
| ATAN2(2, -1)       |
+--------------------+
| 2.0344439357957027 |
+--------------------+

Example 3 – Passing in a Function

In this example I pass in the PI() function as one of the arguments (the PI() function returns a constant value of 3.141593).

SELECT ATAN2(PI(), 1);

Result:

+--------------------+
| ATAN2(PI(), 1)     |
+--------------------+
| 1.2626272556789118 |
+--------------------+

Passing a Single Argument

Providing a single argument to this function appears to work (at least on my system). However, the MySQL documentation doesn’t actually explicitly state whether or not single values are valid (its syntax implies that only two values are valid).

However, the ATAN() function certainly allows for single values to be passed.