Compute the cosine of an angle expressed in radians.
COS(expr)
## Overview Returns the cosine of an angle supplied in radians. Cosine is the horizontal complement of sine and is used in rotation matrices, phase analysis, spherical-law-of-cosines distance formulas, and any workload that projects a vector onto a reference axis. Pair COS with SIN to construct unit vectors on a circle, with ATAN2 to reconstruct angles, and with RADIANS when your columns store angles in degrees. Because the output is bounded to [-1, 1], COS is also useful for cosine similarity between feature vectors after dot-product normalization. ## Behavior - Accepts any finite DOUBLE value. There is no mathematical domain restriction, but very large magnitudes lose precision during argument reduction. - Returns a DOUBLE in the closed interval [-1, 1]. - Returns NULL if the argument is NULL. - Returns NaN for NaN input, 1.0 for input 0. - Integer inputs are implicitly cast to DOUBLE. - Is deterministic across calls within a session and across sessions on the same engine version. ## Numeric precision - Uses the IEEE 754 double-precision cosine routine. Accurate to within one ULP for typical magnitudes. - COS(PI() / 2) returns a tiny non-zero value because pi/2 is not exactly representable in binary floating-point. - For arguments exceeding roughly 1e15 the result may be effectively meaningless due to range-reduction error. ## Compatibility - Conforms to the SQL standard and to IEEE 754 library semantics. - Interchangeable with trigonometric routines in typical mathematical libraries.
| Name | Type | Description |
|---|---|---|
expr | Specifies the angle in radians whose cosine is returned. Accepts any finite DOUBLE value. To pass an angle expressed in degrees, wrap the argument in RADIANS() or use COSD(). |
-- Basic literal: COS of 0 radians
SELECT COS(0);
-- Result: 1.0
-- Cosine at pi evaluates to -1
SELECT COS(PI());
-- Result: -1.0
-- Convert degrees to radians first
SELECT COS(RADIANS(60));
-- Result: 0.5000000000000001
-- Edge case: COS(PI()/2) is very close to 0 (floating-point residue)
SELECT COS(PI() / 2);
-- Result: 6.123233995736766E-17
-- NULL propagation
SELECT COS(NULL);
-- Result: NULL
-- Column use: derive horizontal component of a polar reading
SELECT sensor_id, reading * COS(RADIANS(heading_deg)) AS x_component
FROM iot.telemetry.compass_readings;