In MySQL, the ACOS()
function returns the arc cosine of a number.
You provide the number as an argument when calling the function.
Syntax
The syntax goes like this:
ACOS(X)
Where X
is the value for which you’d like the arc cosine returned. The argument must be a value in the range -1
to 1
. If it’s outside of that range, NULL
is returned.
Example 1 – Basic Usage
Here’s a basic example.
SELECT ACOS(0.1);
Result:
+--------------------+ | ACOS(0.1) | +--------------------+ | 1.4706289056333368 | +--------------------+
Here’s what happens when you provide a value of 1
.
SELECT ACOS(1);
Result:
+---------+ | ACOS(1) | +---------+ | 0 | +---------+
And here’s what happens when you provide a value of -1
.
SELECT ACOS(-1);
Result:
+-------------------+ | ACOS(-1) | +-------------------+ | 3.141592653589793 | +-------------------+
Example 2 – Out-Of-Range Values
As mentioned, providing a value outside the range -1
to 1
returns a NULL value.
SELECT ACOS(2);
Result:
+---------+ | ACOS(2) | +---------+ | NULL | +---------+
Example 3 – Expressions
You can also pass in expressions like this:
SELECT ACOS(0.1 + 0.3);
Result:
+--------------------+ | ACOS(0.1 + 0.3) | +--------------------+ | 1.1592794807274085 | +--------------------+
Example 4 – Zero
Zero is within the accepted range.
SELECT ACOS(0);
Result:
+--------------------+ | ACOS(0) | +--------------------+ | 1.5707963267948966 | +--------------------+
Example 5 – NULL
Passing in NULL
returns NULL
.
SELECT ACOS(NULL);
Result:
+------------+ | ACOS(NULL) | +------------+ | NULL | +------------+
Return the Arc Sine
You can also return the arc sine of a value using the ASIN()
function.