COS()
SQL · Math Functions
Theory
COS returns the trigonometric cosine of an angle. In a right-angled triangle, cosine is the adjacent side divided by the hypotenuse, so like sine it stays between -1 and 1. COS(0) is 1, the cosine of 60 degrees is 0.5 and the cosine of 90 degrees is 0.
COS also expects radians, so convert degrees with RADIANS() or multiply by PI() / 180. A practical way to picture it: if a force of 100 units pulls at 60 degrees from the ground, the part that pulls horizontally is 100 × COS(60°) = 50. COS is often paired with SIN: in the haversine formula for distance between two latitude/longitude points, and in data science where a cyclical value such as hour-of-day is encoded as SIN and COS so that 23:00 and 00:00 end up close together. Because of floating-point maths, COS(RADIANS(90)) returns a tiny number like 6e-17 instead of exactly 0, so round the result.
Why we use it
Many formulas need to split a length or force into horizontal and vertical parts, or measure distance on a sphere. COS gives the horizontal share of an angle and pairs with SIN for the vertical share.
When to use it
Use COS in geospatial distance formulas, direction or force calculations and cyclical feature encoding. Convert degrees to radians first, and ROUND results so tiny floating-point leftovers do not show as odd values.
How it works
- Angle in degrees, e.g. 60
- Convert with RADIANS(60) = 1.0472
- Compute the cosine
- Is the result a tiny leftover like 6e-17? Yes: ROUND it to 0. No: Use the value.
- Return a value between -1 and 1 (0.5)
Syntax
COS(radians)Example
SELECT COS(0);
-- 1Key points
- Input is in radians, not degrees
- Often paired with SIN() in geospatial distance formulas
- COS(0) = 1 and the cosine of 90 degrees = 0
- Floating point gives ~6e-17 instead of 0 for 90 degrees — ROUND the result
- SIN/COS pairs encode cyclical values like hour-of-day for ML features