Menu

SQL Server COS() Function

Updated on

COS() returns the trigonometric cosine of an angle measured in radians. The input must be a float or a type that SQL Server can implicitly convert to float; the return type is float. See Microsoft’s COS() reference.

Syntax

COS(float_expression)

float_expression is the angle in radians.

Usage

The COS() function is commonly used for calculating trigonometric functions such as the cosine value of an angle. In mathematics and engineering, it is often necessary to calculate trigonometric functions to solve various problems such as finding the distance and angle between two objects.

Examples

Here are two examples of using the COS() function.

Example 1

Suppose there is a right triangle with an angle of 45 degrees and a hypotenuse of 10. Now, we need to calculate the length of the base of the triangle. We can use the following query to calculate the length of the base:

DECLARE @angle FLOAT = 45
DECLARE @hypotenuse FLOAT = 10

SELECT @hypotenuse * COS(@angle * PI() / 180) AS adjacent

The output is:

adjacent
7.0710678

Therefore, the length of the base of the triangle is 7.0710678.

Example 2: Evaluate the cosine of a 60-degree angle

Convert 60 degrees to radians before passing the angle to COS():

SELECT COS(RADIANS(CONVERT(float, 60))) AS cosine_of_60_degrees;

The result is 0.5.

Conclusion

Use COS() when the angle is expressed in radians. For a degree-valued input, convert it with RADIANS() first.