PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References
PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References

PostgreSQL ATAN2() Function



The PostgreSQL ATAN2() function returns arc tangent of two numbers. This function uses sign of both numbers to determine the quadrant for the result. The other variant of this function is ATAN2D() which returns the result in degrees.

Syntax

/* returns result in radians */
ATAN2(y, x)

/* returns result in degrees */
ATAN2D(y, x)

Parameters

y Required. Specify the first number used to calculate the arc tangent.
x Required. Specify the second number used to calculate the arc tangent.

Return Value

Returns the arc tangent of two numbers.

Example 1:

The example below shows the usage of ATAN2() function.

SELECT ATAN2(PI(), 2);
Result: 1.0038848218538872

SELECT ATAN2(3, 5);
Result: 0.5404195002705842

SELECT ATAN2(2, 2);
Result: 0.7853981633974483

SELECT ATAN2(2, -2);
Result: 2.356194490192345

SELECT ATAN2(-2, 2);
Result: -0.7853981633974483

SELECT ATAN2(-2, -2);
Result: -2.356194490192345

SELECT ATAN2D(2, 2);
Result: 45

SELECT ATAN2D(2, -2);
Result: 135

SELECT ATAN2D(-2, 2);
Result: -45

SELECT ATAN2D(-2, -2);
Result: -135

Example 2:

Consider a database table called Sample with the following records:

Datayx
Data 11010
Data 2-1010
Data 310-10
Data 4-10-10

To calculate the arc tangent of records of column y and column x, the following query can be used:

SELECT *, ATAN2(y, x) AS ATAN2_Value FROM Sample;

This will produce the result as shown below:

DatayxATAN2_Value
Data 110100.7853981633974483
Data 2-1010-0.7853981633974483
Data 310-102.356194490192345
Data 4-10-10-2.356194490192345

❮ PostgreSQL Functions