trigonometry
Math.tan
Returns the tangent of an angle expressed in radians. Equal to sin(x) / cos(x) and unbounded near π/2 + kπ.
Math.tan(x) Parameters & edge cases
| Case | Behavior |
|---|---|
| x | Angle in RADIANS |
| asymptotes | Tan is undefined at π/2, 3π/2, ...; near these it returns extremely large values |
| range | Returns any real number (-Infinity, +Infinity) |
Examples
console.log(Math.tan(0)); // 0 tan(0) = 0.
console.log(Math.tan(Math.PI / 4)); // 0.9999999999999999 tan(π/4) is mathematically 1 — floating point drops one ULP.
console.log(Math.tan(Math.PI / 2)); // 16331239353195370 Should be Infinity, but Math.PI/2 isn't exact so you get a huge finite number.
console.log(Math.tan(Math.PI)); // -1.2246467991473532e-16 Approximately 0, with the same PI-precision issue.
Gotcha
Math.tan(Math.PI / 2) does NOT return Infinity — it returns a very large finite value (~1.6e16) because π/2 can't be represented exactly.
Related
Math.sin
Returns the sine of an angle expressed in radians. Result is always in the range [-1, 1].
Math.cos
Returns the cosine of an angle expressed in radians. Result is always in [-1, 1].
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.