MySQL ATAN2() Function

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 / b in 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

  1. Quadrant Awareness:

    • ATAN2(a, b) differs from ATAN(a / b) because it uses both parameters to determine the correct quadrant.
    • For example:
      • If a > 0 and b > 0, the angle is in the 1st quadrant.
      • If a > 0 and b < 0, the angle is in the 2nd quadrant.
      • If a < 0 and b < 0, the angle is in the 3rd quadrant.
      • If a < 0 and b > 0, the angle is in the 4th quadrant.
  2. 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
    
  3. Null Handling:

    • If either a or b is NULL, the function returns NULL.

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

Was this article helpful?