TAN()
SQL · Math Functions
Theory
TAN returns the tangent of an angle, which equals SIN divided by COS. In a right-angled triangle, it is the opposite side divided by the adjacent side, so it describes a slope: rise over run. TAN(0) is 0, the tangent of 45 degrees is 1 (a slope that rises one metre per metre) and the tangent of 60 degrees is about 1.7321.
Like the other trigonometric functions it takes radians, so use RADIANS(). Unlike SIN and COS, TAN is not limited to -1..1: it grows without limit as the angle approaches 90 degrees, where it is undefined (COS is zero there). In floating point you get a huge number instead of an error, so guard angles near 90. A typical use is turning an angle into a height: a ramp that rises at 30 degrees over a 10-metre run gains 10 × TAN(30°) ≈ 5.77 metres. The inverse, ATAN (and ATAN2 for coordinates), turns a slope back into an angle and is available in the same databases.
Why we use it
Slopes, gradients and heights from angles are naturally expressed as a tangent. TAN converts an angle into rise per unit of run, which is what height and gradient calculations need.
When to use it
Use TAN for slope and height-from-angle calculations in engineering or geospatial data. Use ATAN or ATAN2 for the reverse (angle from rise and run). Avoid angles at or near 90 degrees.
How it works
- Angle in degrees, e.g. 30
- Convert with RADIANS(30)
- Angle is 90 degrees (odd multiple)? Yes: Undefined: huge number or error. No: Compute SIN / COS.
- Return the slope (0.5774)
Syntax
TAN(radians)Example
SELECT TAN(0);
-- 0Key points
- Input is in radians, not degrees
- Undefined at odd multiples of π/2
- TAN(x) = SIN(x) / COS(x) — rise over run
- Not limited to -1..1; it grows huge near 90 degrees
- ATAN / ATAN2 convert a slope back into an angle