trigonometry
Math.sin
Returns the sine of an angle expressed in radians. Result is always in the range [-1, 1].
Math.sin(x) Parameters & edge cases
| Case | Behavior |
|---|---|
| x | Angle in RADIANS, not degrees — multiply degrees by Math.PI / 180 |
| range | Returns a value in [-1, 1] |
| periodicity | sin(x + 2π) = sin(x); large x accumulate floating-point error |
Examples
console.log(Math.sin(0)); // 0 sin(0) = 0 exactly.
console.log(Math.sin(Math.PI / 2)); // 1 sin(π/2) = 1.
console.log(Math.sin(Math.PI)); // 1.2246467991473532e-16 Not exactly 0! Math.PI is only an approximation of π.
console.log(Math.sin(30 * Math.PI / 180)); // 0.49999999999999994 sin(30°) — remember to convert degrees to radians.
Gotcha
Math.sin(Math.PI) is not 0 — it's ~1.22e-16 because Math.PI is truncated. Use `Math.abs(Math.sin(x)) < 1e-10` when testing for zero.
Related
Math.cos
Returns the cosine of an angle expressed in radians. Result is always in [-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.