MySQL TAN() Function
MySQL TAN() Function: Calculating Trigonometric Tangents
The TAN() function returns the tangent of an angle specified in radians (SIN(x) / COS(x)). It returns a DOUBLE floating-point number for valid numeric inputs.
Engineers use TAN() to compute slope grades from elevation angles, projection offsets in mapping tiles, and triangulation heights. Because MySQL expects radians, degree angles must pass through RADIANS() first.
Syntax
TAN(x)
x: Numeric expression representing an angle in radians.
Practical Example
Suppose demo_employees logs warehouse loading dock ramp angles (incline_deg) and horizontal ramp lengths (run_meters). You can calculate the vertical rise height using run_meters * TAN(RADIANS(incline_deg)).
SELECT
employee_id,
dock_code,
incline_deg,
run_meters,
ROUND(run_meters * TAN(RADIANS(incline_deg)), 2) AS rise_meters
FROM demo_employees
WHERE dock_code IS NOT NULL;
| employee_id | dock_code | incline_deg | run_meters | rise_meters |
|---|---|---|---|---|
| 51 | DOCK-1 | 15.0 | 10.0 | 2.68 |
| 52 | DOCK-2 | 45.0 | 4.0 | 4.00 |
| 53 | DOCK-3 | 0.0 | 8.0 | 0.00 |
At 45.0 degrees (PI()/4 radians), TAN(RADIANS(45)) equals 1, so a 4.0-meter run rises 4.00 meters.
Common Mistakes & NULL Behavior
- Using degrees without RADIANS(): Calling
TAN(45)returns1.6198(tangent of45radians), not1.0. Always wrap degree values withRADIANS(). - Calling TAN(PI() / 2): Mathematically, tangent approaches infinity at 90 degrees (
PI() / 2). In MySQL, becausePI()is a finiteDOUBLE,TAN(PI() / 2)does not error—it returns a huge number (1.633123935319537e16). Filter out90-degree angles before multiplying. - NULL inputs: Passing
NULLtoTAN(NULL)returnsNULL.
Frequently Asked Questions
Does TAN(PI() / 2) raise an error in MySQL?
No. Due to finite floating-point representation of PI(), COS(PI() / 2) is tiny (6.12e-17) rather than exact 0, so TAN(PI() / 2) returns a very large DOUBLE instead of an error.
How do I reverse TAN() back into an angle?
Pass the tangent ratio into ATAN(ratio) to get radians, or DEGREES(ATAN(ratio)) to recover degrees.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.