The ATAN2() function calculates the arc tangent (inverse tangent) of two numbers, considering their signs to determine the appropriate quadrant of the resulting angle.
Syntax
ATAN2(a, b)
Definition and Usage
- The
ATAN2()function returns the angle in radians between the positive x-axis and the line to the point(b, a)in a Cartesian plane. - It uses the signs of both parameters to determine the quadrant of the angle.
- The result is in the range [−π,π][- \pi, \pi].
Parameter Values
| Parameter | Description |
|---|---|
a |
Required. The y-coordinate (numerator). |
b |
Required. The x-coordinate (denominator). |
Technical Details
- Works in: From MySQL 4.0
- Result: Returns the arc tangent of the ratio
a / bin radians, taking into account the quadrant.
Examples
Example 1: Arc tangent of two positive values
SELECT ATAN2(0.50, 1);
Result:
0.4636476090008061
Example 2: Arc tangent with a negative numerator
SELECT ATAN2(-0.8, 2);
Result:
-0.3805063771123649
Example 3: Arc tangent with both negative values
SELECT ATAN2(-1, -1);
Result:
-2.356194490192345
Example 4: Arc tangent with a = 0
SELECT ATAN2(0, -1);
Result:
3.141592653589793
Usage Notes
-
Quadrant Awareness:
ATAN2(a, b)differs fromATAN(a / b)because it uses both parameters to determine the correct quadrant.- For example:
- If
a > 0andb > 0, the angle is in the 1st quadrant. - If
a > 0andb < 0, the angle is in the 2nd quadrant. - If
a < 0andb < 0, the angle is in the 3rd quadrant. - If
a < 0andb > 0, the angle is in the 4th quadrant.
- If
-
Radians to Degrees Conversion: Multiply the result by 180π\frac{180}{\pi} to convert to degrees:
SELECT ATAN2(0.50, 1) * (180 / PI());Result in degrees:
26.56505117707799 -
Null Handling:
- If either
aorbisNULL, the function returnsNULL.
- If either
Example Query with Conversion
To get the arc tangent of multiple pairs of values in both radians and degrees:
SELECT
ATAN2(0.50, 1) AS Radians,
ATAN2(0.50, 1) * (180 / PI()) AS Degrees;
Result:
Radians: 0.4636476090008061
Degrees: 26.56505117707799