trigonometry
Math.cos
Returns the cosine of an angle expressed in radians. Result is always in [-1, 1].
Math.cos(x) Parameters & edge cases
| Case | Behavior |
|---|---|
| x | Angle in RADIANS |
| range | Returns a value in [-1, 1] |
| phase relation | cos(x) === sin(x + π/2) |
Examples
console.log(Math.cos(0)); // 1 cos(0) = 1 exactly.
console.log(Math.cos(Math.PI)); // -1 cos(π) = -1.
console.log(Math.cos(Math.PI / 2)); // 6.123233995736766e-17 Should be 0 but Math.PI is inexact.
console.log(Math.cos(60 * Math.PI / 180)); // 0.5000000000000001 cos(60°) — nearly exactly 0.5.
Gotcha
Like Math.sin(Math.PI), Math.cos(Math.PI / 2) is not exactly 0. Never use === when checking against expected trig values.
Related
Math.sin
Returns the sine of an angle expressed in radians. Result is always in the range [-1, 1].
Math.tan
Returns the tangent of an angle expressed in radians. Equal to sin(x) / cos(x) and unbounded near π/2 + kπ.
Math.atan2
Returns the angle in radians between the positive x-axis and the point (x, y), in the range (-π, π]. Note the argument order: y first, then x.