MySQL COS() Function
MySQL COS() Function: Calculating Cosine Values in SQL
The COS() function returns the trigonometric cosine of an angle measured in radians. The output is a DOUBLE value ranging from -1 to 1.
Developers mostly use COS() in spatial queries—such as calculating Earth surface distances from latitude coordinates—or in engineering analytics involving periodic waveforms. Because MySQL expects radians, degree inputs must be converted first.
Syntax
COS(x)
x: Numeric expression representing an angle in radians.
Practical Example
Suppose demo_customers stores customer latitude in degrees (latitude_deg). In geographic distance math, one degree of longitude shrinks by COS(RADIANS(latitude_deg)). Let's inspect that scaling factor per customer city.
SELECT
customer_id,
city,
latitude_deg,
ROUND(RADIANS(latitude_deg), 4) AS lat_radians,
ROUND(COS(RADIANS(latitude_deg)), 4) AS lon_scale_factor
FROM demo_customers
WHERE customer_id IN (1, 2, 3, 4);
| customer_id | city | latitude_deg | lat_radians | lon_scale_factor |
|---|---|---|---|---|
| 1 | Quito | 0.00 | 0.0000 | 1.0000 |
| 2 | Miami | 25.76 | 0.4496 | 0.9006 |
| 3 | London | 51.51 | 0.8990 | 0.6224 |
| 4 | Reykjavik | NULL | NULL | NULL |
At the equator (0.00 degrees), COS(0) equals 1.0000. As latitude increases toward London, the cosine shrinks to 0.6224.
Common Mistakes & NULL Behavior
- Passing degrees without converting: Writing
COS(90)computes the cosine of90radians (-0.44807), not90degrees (0). Always wrap degree columns inRADIANS(deg). - Floating-point near-zero results: Because
PI()is a finite approximation,COS(PI() / 2)returns6.123233995736766e-17instead of exact0. UseROUND(COS(expr), 6)before comparing to0. - NULL inputs: Passing
NULLtoCOS()returnsNULL, as shown for row4above.
Frequently Asked Questions
What happens if I pass a huge number to COS()?
COS() accepts any finite numeric input without domain errors and returns a value in [-1, 1]. Passing NULL is the only normal way to get NULL.
How do I calculate COS() for 60 degrees in MySQL?
Run SELECT COS(RADIANS(60));, which returns 0.5000000000000001 (or 0.5 when rounded).
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.