How Atan2d() Works in PostgreSQL

In PostgreSQL, atan2d() is a trigonometric function that returns the arctangent, or inverse tangent, of the specified x and y coordinates in degrees.

Syntax

The syntax goes like this:

atan2d(y, x)

Where x and y are double precision values.

The arctangent is the angle from the x-axis to a line containing the origin (0, 0) and a point with coordinates (x, y)

Example

Here’s an example to demonstrate how it works.

SELECT atan2d(1, 1);

Result:

45

This returned the arctangent of the point 1,1 in degrees.

Negative Argument

The argument can also be negative.

SELECT atan2d(-1, -1);

Result:

-135

This returned the arctangent of the point -1,-1 in degrees.

Atan2d() vs Atand(x/y)

Using atan2d(x, y) is functionally equivalent to using atand(x/y).

However, one difference is that, if you specify a y value of zero, atan2d() won’t return an error, whereas atand() will.

Here’s an example using atan2d():

SELECT atan2d(1, 0);

Result:

90

And here’s what we get with atand() when trying to divide by zero:

SELECT atand(1/0);

Result:

ERROR: division by zero

Return the Angle in Radians

As mentioned, atan2d() returns its argument in degrees. To get it in radians, use the atan2() function.

The atan2() function works exactly the same as atan2d(), except that its argument is returned in radians instead of degrees.