Different ways to get ARC Tangent(ATAN, ATAN2) values of two variables in MySQL8?

With MySQL, we can do complex calculations very easily with the help of inbuilt mathematical functions. ATAN() helps in getting the arc tangent of the input number. Now let’s start with these.


ATAN(B, A) and ATAN2(B, A) is a mathematical function. It returns the arc tangent  of two variables .i.e A and B. It is the same as calculation B / A, except that the sign of both the argument determines the quadrant of the result.

It returns NULL, if input is passed as NULL.

It returns Warning, if any string argument is passed.


MySQL ATAN() and ATAN2() : Syntax

ATAN (B, A);
OR
ATAN2 (B, A);

MySQL ATAN() and ATAN2() : Parameter

Name, Required /Optional, Type, Description
number, Required, Double, It represents a valid number.


MySQL ATAN() and ATAN2() : Output

Return, Description
NULL, if the argument is NULL.
Double, It returns the arc tangent of the input number.


MySQL ATAN() and ATAN2() Available from : MySQL 4.0


ATAN() and ATAN2() Example 1 : Very Basic example

Below is a very easy example. See the value when we pass the input as A as-4, B as 3. Both functions are returning the same value.

mysql> SELECT ATAN(-4,3);
+---------------------+
| ATAN(-4,3) |
+---------------------+
| -0.9272952180016122 |
+---------------------+
1 row in set (0.03 sec)

mysql> SELECT ATAN2(-4,3);
+---------------------+
| ATAN2(-4,3) |
+---------------------+
| -0.9272952180016122 |
+---------------------+
1 row in set (0.00 sec)

ATAN() and ATAN2() Example 2 :

Below are some more examples.

mysql> SELECT ATAN(PI(),-3);
+-------------------+
| ATAN(PI(),-3) |
+-------------------+
| 2.333143860959771 |
+-------------------+
1 row in set (0.04 sec)

mysql> SELECT ATAN(8,PI());
+--------------------+
| ATAN(8,PI()) |
+--------------------+
| 1.1965996462722117 |
+--------------------+
1 row in set (0.00 sec)

mysql> SELECT ATAN2(PI(),-3);
+-------------------+
| ATAN2(PI(),-3) |
+-------------------+
| 2.333143860959771 |
+-------------------+
1 row in set (0.03 sec)

mysql> SELECT ATAN2(8,PI());
+--------------------+
| ATAN2(8,PI()) |
+--------------------+
| 1.1965996462722117 |
+--------------------+
1 row in set (0.00 sec)

ATAN() and ATAN2() Example 3 : It will return Warning if any string argument is passed.

See the below example :

mysql> SELECT ATAN('test',-3);
+-------------------+
| ATAN('test',-3) |
+-------------------+
| 3.141592653589793 |
+-------------------+
1 row in set, 1 warning (0.00 sec)

mysql> SHOW WARNINGS;
+---------+------+------------------------------------------+
| Level | Code | Message |
+---------+------+------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: 'test' |
+---------+------+------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT ATAN2(5,'test');
+--------------------+
| ATAN2(5,'test') |
+--------------------+
| 1.5707963267948966 |
+--------------------+
1 row in set, 1 warning (0.00 sec)

mysql> SHOW WARNINGS;
+---------+------+------------------------------------------+
| Level | Code | Message |
+---------+------+------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: 'test' |
+---------+------+------------------------------------------+
1 row in set (0.00 sec)

You can see the warning message with the help of the ‘SHOW WARNINGS’ command. Please refer above code for the same.


ATAN() and ATAN2() Example 4 : NULL arguments

If either of the argument is NULL, it will return NULL. See the below example :

mysql> SELECT ATAN(NULL,-3);
+---------------+
| ATAN(NULL,-3) |
+---------------+
| NULL |
+---------------+
1 row in set (0.00 sec)

mysql> SELECT ATAN(8,NULL);
+--------------+
| ATAN(8,NULL) |
+--------------+
| NULL |
+--------------+
1 row in set (0.00 sec)

mysql> SELECT ATAN2(NULL,-8);
+----------------+
| ATAN2(NULL,-8) |
+----------------+
| NULL |
+----------------+
1 row in set (0.00 sec)

mysql> SELECT ATAN2(6,NULL);
+---------------+
| ATAN2(6,NULL) |
+---------------+
| NULL |
+---------------+
1 row in set (0.00 sec)

ATAN() Example 5 : Using expressions

Here are some more examples to use the expression with the ATAN() function :

mysql> SELECT ATAN(.2,.5);
+--------------------+
| ATAN(.2,.5) |
+--------------------+
| 0.3805063771123649 |
+--------------------+
1 row in set (0.03 sec)

With expression, it’s returning the same value as the above example.

mysql> SELECT ATAN(.1+.1,.2+.3);
+--------------------+
| ATAN(.1+.1,.2+.3) |
+--------------------+
| 0.3805063771123649 |
+--------------------+
1 row in set (0.03 sec)

Here are some more examples to use the expression with the ATAN2() function :

mysql> SELECT ATAN2(9,5);
+--------------------+
| ATAN2(9,5) |
+--------------------+
| 1.0636978224025597 |
+--------------------+
1 row in set (0.00 sec)

With expression, it’s returning the same value as the above example.

mysql> SELECT ATAN2(2+7,1+4);
+--------------------+
| ATAN2(2+7,1+4) |
+--------------------+
| 1.0636978224025597 |
+--------------------+
1 row in set (0.00 sec)

You can see in the below screen that both inputs yield the same results.

mysql> SELECT (.2*2),(.5*2);
+--------+--------+
| (.2*2) | (.5*2) |
+--------+--------+
| 0.4 | 1.0 |
+--------+--------+
1 row in set (0.00 sec)

mysql> SELECT ATAN(.2*2,.5*2);
+--------------------+
| ATAN(.2*2,.5*2) |
+--------------------+
| 0.3805063771123649 |
+--------------------+
1 row in set (0.01 sec)

mysql> SELECT ATAN2(.2*2,.5*2);
+--------------------+
| ATAN2(.2*2,.5*2) |
+--------------------+
| 0.3805063771123649 |
+--------------------+
1 row in set (0.01 sec)

We are getting the same values when we are using expressions, rather than using the number directly.


Related articles : ABS(), ACOS(), MySQL Maths.

You may also like...