How the COS() function works in Mariadb?

The COS() function is a mathematical function that returns the cosine of a given angle. The function returns a decimal value between -1 and 1, representing the cosine of the angle in radians.

Posted on

The COS() function is a mathematical function that returns the cosine of a given angle. The function returns a decimal value between -1 and 1, representing the cosine of the angle in radians. The function can handle angles in degrees, by using the RADIANS() function to convert them to radians.

Syntax

The syntax of the COS() function is as follows:

COS(angle)

The function takes one argument, angle, which is the angle to be calculated. The argument can be a number or an expression that evaluates to a number. The function returns NULL if the argument is NULL or invalid.

Examples

Example 1: Calculating the cosine of a zero angle

The following example uses the COS() function to calculate the cosine of a zero angle.

SELECT COS(0);

The output is:

+--------+
| COS(0) |
+--------+
|      1 |
+--------+

The output shows that the cosine of a zero angle is 1, as expected.

Example 2: Calculating the cosine of a 90-degree angle

The following example uses the COS() function to calculate the cosine of a 90-degree angle. The angle is converted to radians by using the RADIANS() function.

SELECT COS(RADIANS(90));

The output is:

+-----------------+
| COS(RADIANS(90)) |
+-----------------+
|               0 |
+-----------------+

The output shows that the cosine of a 90-degree angle is 0, as expected.

Example 3: Calculating the cosine of a negative angle

The following example uses the COS() function to calculate the cosine of a negative angle. The angle is -45 degrees, which is equivalent to 315 degrees.

SELECT COS(RADIANS(-45));

The output is:

+------------------+
| COS(RADIANS(-45)) |
+------------------+
| 0.70710678118655 |
+------------------+

The output shows that the cosine of a negative angle is the same as the cosine of the corresponding positive angle, as expected.

Example 4: Calculating the cosine of a large angle

The following example uses the COS() function to calculate the cosine of a large angle. The angle is 720 degrees, which is equivalent to 0 degrees.

SELECT COS(RADIANS(720));

The output is:

+------------------+
| COS(RADIANS(720)) |
+------------------+
|                1 |
+------------------+

The output shows that the cosine of a large angle is the same as the cosine of the angle modulo 360 degrees, as expected.

Example 5: Calculating the cosine of an invalid angle

The following example uses the COS() function to calculate the cosine of an invalid angle. The angle is a string that cannot be converted to a number.

SELECT COS('Hello');

The output is:

+-----------+
| COS('Hello') |
+-----------+
|      NULL |
+-----------+

The output shows that the function returns NULL, as expected. The function returns NULL if the argument is NULL or invalid.

There are some other functions that are related to the COS() function in Mariadb. They are:

  • SIN(): This function returns the sine of a given angle.
  • TAN(): This function returns the tangent of a given angle.
  • ACOS(): This function returns the arc cosine of a given value.
  • RADIANS(): This function converts an angle in degrees to radians.
  • DEGREES(): This function converts an angle in radians to degrees.

Conclusion

The COS() function is a useful function to calculate the cosine of a given angle. It returns a decimal value between -1 and 1, representing the cosine of the angle in radians. It can handle angles in degrees, by using the RADIANS() function to convert them to radians. It is similar to the SIN() and TAN() functions, but with a different trigonometric ratio. It is also related to some other functions that provide inverse or conversion operations on angles or values.