MySQL ATAN() and ATAN2() Functions – Finding the Arc Tangent of an Angle in MySQL

ATAN ATAN2 Functions

In this tutorial, we will study the MySQL ATAN() and ATAN2() functions. In high school, you must have studied the arc tangent of an angle in trigonometry. Since trigonometry is so widely used across a number of fields, MySQL provides us with the MySQL ATAN() and ATAN2() functions.

The ATAN() function is used to find the arc tangent of a value. It can also return the arc tangent of two values.

The ATAN2() function is used to find the arc tangent of two values. The two values are provided as comma-separated arguments and the function returns the arc tangent value of argument1 divided by argument2. 

Recommended Read – MySQL TAN() and COS() functions


Syntax of MySQL ATAN()

ATAN(number);Code language: SQL (Structured Query Language) (sql)

Where ‘number’ is the value of an angle (in radians) whose arc tangent has to be found out.

OR

ATAN(X, Y);Code language: SQL (Structured Query Language) (sql)

Where ‘X’ is the first number and ‘Y’ is the second number and the function returns the arc tangent of the value ‘X/Y’.


Syntax of MySQL ATAN2()

ATAN2(X, Y);Code language: SQL (Structured Query Language) (sql)

Where ‘X’ is the first number and ‘Y’ is the second number and the function returns the arc tangent of the value ‘X/Y’.


Example of MySQL ATAN()

Let us start by looking at some basic examples of the ATAN() function. Consider the below queries.

SELECT ATAN(2.3); 
SELECT ATAN(1); 
SELECT ATAN(0.052);Code language: SQL (Structured Query Language) (sql)

And we get the output as –

MySQL ATAN Basic Example

ATAN() With Negative Values

We can also pass negative values in the ATAN() function. Let us see a few examples of this. Consider the below queries.

SELECT ATAN(-9); 
SELECT ATAN(-3.2); 
SELECT ATAN(-1);Code language: SQL (Structured Query Language) (sql)

And we get the output as –

MySQL ATAN Negative Example

ATAN() With Expressions

We can also pass mathematical expressions as parameters to the ATAN() function. Let us see an example of this using the below queries.

SELECT ATAN(3.8+0.0056); 
SELECT ATAN(0.2*0.06);Code language: SQL (Structured Query Language) (sql)

And we get the output as follows.

Atan Expression

ATAN() With Two Arguments

Now let us try using the second syntax of the ATAN() function where we can pass two arguments. Consider the below queries.

SELECT ATAN(6, 2); 
SELECT ATAN(0.2, 5); 
SELECT ATAN(-5, 8);Code language: SQL (Structured Query Language) (sql)

These queries will return the arc tangent of the values – 6/2, 0.2 / 5, and -5/8. The output is –

Atan Two Arguments 1

To get more clarity, let us see the below example. Since 6 divided by 2 = 3, we will find the arctangent of 3 and then the arctangent of 6/2 using the two-argument syntax of ATAN(). Consider the query – 

SELECT ATAN(6, 2), ATAN(3);Code language: SQL (Structured Query Language) (sql)

And we get the output as –

Atan Two Arguments 2

Therefore, when two arguments X and Y are passed to ATAN(), it returns the arc tangent of the value X/Y.

ATAN() With PI()

We can also pass mathematical functions like MySQL PI() to ATAN(). Consider the below query.

SELECT ATAN(PI()/2);Code language: SQL (Structured Query Language) (sql)

And we get the output as –

Atan Pi

Examples of MySQL ATAN2()

Let us now move on to some of the basic examples of MySQL ATAN2(). Consider the below queries.

SELECT ATAN2(3, 2); 
SELECT ATAN2(6, 2);Code language: SQL (Structured Query Language) (sql)

The function returns the arc tangent of the values 3/2 and 6/2 respectively. And we get the output as –

MySQL Atan2 Basic Example 1

Similarity of ATAN2() and ATAN() With Two Arguments

ATAN() with two arguments is equal to the ATAN2() function. However, we highly recommend you to use ATAN2() when dealing with two values.

Let us now see how ATAN() with two arguments is similar to ATAN2(). Suppose we have to find the arctangent of 6/2. We can do this in three ways.

First, we reduce 6/2 to 3 and pass 3 as a single argument to ATAN(). Second, we can pass 6 and 2 as two arguments to the ATAN() function, and lastly, we can pass 6 and 2 to the ATAN2() function. Let us see the query for this.

SELECT ATAN(3), ATAN(6, 2), ATAN2(6, 2);Code language: SQL (Structured Query Language) (sql)

And we get the output as –

MySQL Atan2 Basic Example 2

ATAN2() With Negative Values

We can also pass negative values to the ATAN2() function. Consider the below queries.

SELECT ATAN2(-5, 6); 
SELECT ATAN2(-3, 5); 
SELECT ATAN2(8, -2);Code language: SQL (Structured Query Language) (sql)

And we get the output as follows –

Atan2 Negative Example

ATAN2() With PI()

We can also pass mathematical functions like PI() to the ATAN2() function. Let us see an example of this.

SELECT ATAN2(PI(), 4);Code language: SQL (Structured Query Language) (sql)

And we get an output as follows.

Atan2 Pi

Using ATAN() and ATAN2() With Tables

Consider the below ‘Angles’ table.

Angles Table
Angles Table

MySQL ATAN() Example With Tables

Let us display the Angle column and the corresponding arc tangent value of the angle under the alias ArcTangent using the SELECT statement. The query is –

SELECT Angle, ATAN(Angle) AS ArcTangent FROM Angles;Code language: SQL (Structured Query Language) (sql)

And we get the output as follows –

Atan Table Example

MySQL ATAN2() Example With Tables

Lastly, let us display the Angle column and the arc tangent of half the angle value in the Angle column. The query for this is –

SELECT Angle, ATAN2(Angle, 2) AS ‘ArcTangent/2FROM Angles;Code language: SQL (Structured Query Language) (sql)

And we get the output as –

Atan2 Table Example

Conclusion

Finding the arc tangent of an angle is an important trigonometric operation. You will find yourself using the ATAN() and ATAN2() functions every time you deal with data with trigonometric operations. Prefer using ATAN2() when dealing with two values and ATAN() when using a single value.


References