How to return COS() values in MySQL8?

With MySQL, we can do complex calculations very easily with the help of inbuilt mathematical functions. COS(number) helps in getting the cosine value, when the input number value is given in radians.

The cosine is the trigonometric function that is equal to the ratio of the side adjacent to an acute angle (in a right-angled triangle) to the hypotenuse.

Now let’s start with these.


COS(X) is a mathematical function. It returns the cosine of number X. Where X is the argument, whose value will be passed by the user.

It returns NULL, if X is passed as NULL.

It returns Warning, if any string argument is passed.


MySQL COS() : Syntax

COS ( X )

MySQL COS() : Parameter

Name, Required /Optional,Type, Description
number , Required, Double , It represents valid number.


MySQL COS() : Output

Return, Description
NULL, if the argument is NULL.
Double, It returns arc cosine of number.


MySQL COS()  Available from : MySQL 4.0


COS() Example 1 :

See the below example. See, it returns 1, when 0 is passed.

mysql> SELECT COS(0);
+--------+
| COS(0) |
+--------+
| 1 |
+--------+
1 row in set (0.05 sec)

Now, Lets change the input value to -1. See what it returns.

mysql> SELECT COS(-1);
+--------------------+
| COS(-1) |
+--------------------+
| 0.5403023058681398 |
+--------------------+
1 row in set (0.00 sec)

See the below value When 1 is passed as an input.

mysql> SELECT COS(1);
+--------------------+
| COS(1) |
+--------------------+
| 0.5403023058681398 |
+--------------------+
1 row in set (0.00 sec)

COS() Example 2 : Using it with PI()

We are using the PI() function to get the value of cosine with it. I get the below value.

mysql> SELECT COS(PI());
+-----------+
| COS(PI()) |
+-----------+
| -1 |
+-----------+
1 row in set (0.10 sec)

COS() Example 3 : Some more examples

Below are some more examples.

mysql> SELECT COS(45);
+--------------------+
| COS(45) |
+--------------------+
| 0.5253219888177297 |
+--------------------+
1 row in set (0.00 sec)

mysql> SELECT COS(-45);
+--------------------+
| COS(-45) |
+--------------------+
| 0.5253219888177297 |
+--------------------+
1 row in set (0.07 sec)

mysql> SELECT COS(88.56);
+--------------------+
| COS(88.56) |
+--------------------+
| 0.8279210325790082 |
+--------------------+
1 row in set (0.02 sec)

mysql> SELECT COS(-88.56);
+--------------------+
| COS(-88.56) |
+--------------------+
| 0.8279210325790082 |
+--------------------+
1 row in set (0.00 sec

COS() Example 3 : return warning if any string argument is passed.

See the below example :

mysql> SELECT COS('test');
+-------------+
| COS('test') |
+-------------+
| 1 |
+-------------+
1 row in set, 1 warning (0.10 sec)

mysql> SHOW WARNINGS;
+---------+------+------------------------------------------+
| Level | Code | Message |
+---------+------+------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: 'test' |
+---------+------+------------------------------------------+
1 row in set (0.10 sec)

You can see the warning message with the help of the ‘SHOW WARNINGS’ command. Please refer above code for the same.


COS() Example 4 : NULL arguments

If the argument is NULL, it will return NULL. See the below example :

mysql> SELECT COS(NULL);
+-----------+
| COS(NULL) |
+-----------+
| NULL |
+-----------+
1 row in set (0.01 sec)

Related articles : PI(), ABS(), MySQL Maths.

You may also like...