MySQL ACOS() – Easy way to find the arc cosine of a value

ACOS Function

In this tutorial, we will study the MySQL ACOS() function. In maths, you must have come across the function – acos. The arc cosine of a number x is the inverse cosine function of x where x is a number between -1 and 1. It returns the angle whose cosine is a given number. Arc cosine is one of the inverse trigonometric functions.

Like all other trigonometric operations, acos is widely used and that’s why MySQL provides us with the ACOS() function.

ACOS() returns the arc cosine of a number. As evident, it is the inverse of the COS() function. ACOS() returns the angle (in radians) whose cosine value is passed as a parameter to it. 


Syntax of MySQL ACOS()

ACOS(number);

Where ‘number’ is a number whose arc cosine is to be found. Note that, ‘number’ should have a value between -1 and 1. 


Examples of MySQL ACOS()

Let us start with some basic examples of the ACOS() function. We have the following three expressions – acos 0.56, acos 1 and acos -1. Let us find their values in MySQL using the ACOS() function with the SELECT statement.

SELECT ACOS(0.56); 
SELECT ACOS(1); 
SELECT ACOS(-1);

And we get the output as,

MySQL ACOS Basic Example

MySQL ACOS() With Numbers Out Of Range

We saw in the syntax that the ‘number’ parameter should be a value between -1 (inclusive) and 1 (inclusive). What if you pass a value that is not in this range? That is, the ‘number’ value is greater than 1 or less than -1. Let us see what ACOS() returns in such a case using the below example.

SELECT ACOS(-185); 
SELECT ACOS(56);

And the output is,

MySQL ACOS Out Of Range

When we pass numbers not in the range -1 and 1 to ACOS(), it will return NULL. 

MySQL ACOS() With Expressions

We can also pass mathematical expressions as parameters inside the ACOS() function. Suppose you have the following two expressions – acos(0.025*0.2) and acos (0.45+0.2). We can do this in MySQL using the ACOS() using the below queries.

SELECT ACOS(0.025*0.2); 
SELECT ACOS(0.45+0.2);

And the output is,

Acos Expressions

ACOS() With Zero

Let us find the arc cosine of zero now.

SELECT ACOS(0);

And the output is,

Acos Zero

ACOS() With NULL

ACOS() returns NULL if the number passed to it is NULL. We can demonstrate this using the below example.

SELECT ACOS(NULL);

And we get the output as follows – 

Acos Null

ACOS() Is The Inverse Of COS()

As mentioned earlier in the introduction, ACOS() is the inverse of COS(). Suppose the cosine of y is equal to x. That is, cos y = x

Then the arc cosine of x is equal to the inverse cosine function of x, which is therefore, equal to y: arc cos x = cos-1 x = y

Let us demonstrate this using the below example.

SELECT ACOS(COS(0.5));

First, we get the value of COS(0.5). Now the result of the COS() function is passed into the ACOS() function.

Since ACOS() is the inverse of COS(), it will return the numerical value (approximately) that was passed to the COS() function (0.5 in this example) as the result. In the below output we get 0.49999999 which is rougly equal to 0.5. 

Acos Inverse

Using ACOS() With Tables

Consider the below ‘Angles’ table.

Acos Angles Table
Angles Table

Building on the previous example about how ACOS() is the inverse of COS(), let us write a query that displays the Angle column and the arc cosine of the values in the CosineOfAngle column. We use aliases to make our output readable. The query is – 

SELECT Angle, ACOS(CosineOfAngle) AS ArcCosine FROM Angles;

And the output is,

Acos Table Example

As you can see, the values in both columns are roughly equal to each other.


Conclusion

Finding the arc cosine of an angle is an important trigonometric operation. You will find yourself using the ACOS() function every time you deal with data with trigonometric operations.


References