How the ATAN2() function works in Mariadb?

The ATAN2() function is a mathematical function that returns the arc tangent of two numbers. The arc tangent is the angle whose tangent is the ratio of the two numbers.

Posted on

The ATAN2() function is a mathematical function that returns the arc tangent of two numbers. The arc tangent is the angle whose tangent is the ratio of the two numbers. The ATAN2() function is useful for calculating the angle of a slope or the direction of a vector.

Syntax

The syntax of the ATAN2() function is as follows:

ATAN2(Y, X)

The parameter Y and X are numeric values that represent the coordinates of a point on a plane. The function returns the angle in radians, ranging from $$-\pi$$ to $$\pi$$, of the vector from the origin to the point. This is equivalent to ATAN(Y / X), but it can handle the cases where X is zero or negative.

If any of the parameters are NULL, the function returns NULL.

Examples

Example 1: Positive arguments

In this example, we use the ATAN2() function to get the angle of a vector with coordinates (3, 4).

SELECT ATAN2(4, 3) AS Angle;

The output is:

+--------+
| Angle  |
+--------+
| 0.9273 |
+--------+

The angle is approximately 0.9273 radians, which is equivalent to $$\frac{\pi}{3.4}$$ radians or 53.13 degrees.

Example 2: Negative arguments

In this example, we use the ATAN2() function to get the angle of a vector with coordinates (-3, -4).

SELECT ATAN2(-4, -3) AS Angle;

The output is:

+---------+
| Angle   |
+---------+
| -2.2143 |
+---------+

The angle is approximately -2.2143 radians, which is equivalent to $$-\frac{7\pi}{3.4}$$ radians or -126.87 degrees.

Example 3: Zero argument

In this example, we use the ATAN2() function to get the angle of a vector with coordinates (0, 1).

SELECT ATAN2(1, 0) AS Angle;

The output is:

+--------+
| Angle  |
+--------+
| 1.5708 |
+--------+

The angle is approximately 1.5708 radians, which is equivalent to $$\frac{\pi}{2}$$ radians or 90 degrees.

Some of the functions that are related to the ATAN2() function are:

  • TAN(): This function returns the tangent of an angle.
  • ATAN(): This function returns the arc tangent of a number or the angle of a vector.
  • ASIN(): This function returns the arc sine of a number.
  • ACOS(): This function returns the arc cosine of a number.

Conclusion

The ATAN2() function is a useful function that can be used to get the arc tangent of two numbers or the angle of a vector. It can handle the cases where the denominator is zero or negative, and it returns the angle in radians, which can be converted to degrees if needed. The function follows the mathematical convention and is compatible with other trigonometric functions and databases.