How Acos() Works in PostgreSQL

In PostgreSQL, acos() is a mathematical function that returns the inverse cosine of the specified expression, measured in radians.

The inverse cosine is also known as the arccosine.

Syntax

The syntax goes like this:

acos(x)

Where x is a double precision value.

Example

Here’s an example to demonstrate how it works.

SELECT acos(0.5);

Result:

1.0471975511965976

This is the equivalent of 60 degrees.

Pi

Providing -1 returns π.

SELECT acos(-1);

Result:

3.141592653589793

Out of Range Error?

The argument must range from -1.00 to 1.00. Any values outside this range will result in an error.

SELECT acos(1.01);

Result:

ERROR: input is out of range

And here it is with a negative expression.

SELECT acos(-1.01);

Result:

ERROR: input is out of range

Argument of Zero

An argument of zero returns the equivalent of π/2, which is the equivalent to 90 degrees.

SELECT acos(0);

Result:

1.5707963267948966

Cosine of Arccosine

Since arccosine is the inverse function of cosine, the cosine of arccosine of x is equal to x.

We can pass acos() to the cos() function as an argument in order to demonstrate this.

SELECT cos(acos(0.45));

Result:

0.45

Return the Arccosine in Degrees

As mentioned, acos() returns the arccosine in radians. To return the arccosine in degrees, use the acosd() function.

The acosd() function works exactly the same as acos(), except that it returns its result in degrees instead of radians.