MySQL COT() Function
MySQL COT() Function: Computing Cotangent Values in SQL
The COT() function returns the trigonometric cotangent of an angle given in radians. Mathematically, COT(x) equals 1 / TAN(x) or COS(x) / SIN(x).
While less common than SIN() or COS(), COT() appears in survey geometry, roof-pitch formulas, and optical angle calculations. It returns a DOUBLE floating-point number for any valid non-zero angle.
Syntax
COT(x)
x: Numeric expression representing an angle in radians. Must not evaluate to0(or an exact multiple ofPI()whereSIN(x)hits zero).
Practical Example
Suppose demo_orders tracks structural bracket angles (pitch_deg) for custom solar mounts. You can compute the cotangent ratio safely while guarding against zero-degree angles with NULLIF().
SELECT
order_id,
pitch_deg,
ROUND(RADIANS(pitch_deg), 4) AS pitch_rad,
ROUND(COT(NULLIF(RADIANS(pitch_deg), 0)), 4) AS cotangent_ratio
FROM demo_orders
WHERE order_id BETWEEN 401 AND 404;
| order_id | pitch_deg | pitch_rad | cotangent_ratio |
|---|---|---|---|
| 401 | 45.0 | 0.7854 | 1.0000 |
| 402 | 30.0 | 0.5236 | 1.7321 |
| 403 | 90.0 | 1.5708 | 0.0000 |
| 404 | 0.0 | 0.0000 | NULL |
At 45.0 degrees, TAN(RADIANS(45)) is 1, so COT() returns 1.0000. On row 404, NULLIF(..., 0) turns 0 into NULL so the query finishes cleanly instead of failing.
Common Mistakes & NULL Behavior
- Passing zero to COT(0): Unlike
MOD(n, 0)orSQRT(-1), callingCOT(0)does not returnNULL. MySQL immediately throwsERROR 1690 (22003): DOUBLE value is out of range in 'cot(0)'. Always wrap risky columns inNULLIF(angle, 0). - Forgetting radian conversion:
COT(45)computes the cotangent of45radians (0.6174), not45degrees (1.0). Convert degrees withRADIANS()first. - NULL propagation: Passing
NULLtoCOT(NULL)returnsNULLsafely without throwing an error.
Frequently Asked Questions
Why does MySQL throw an error on COT(0) instead of returning NULL?
At 0 radians, TAN(0) is 0, making 1 / 0 infinite. MySQL treats infinite trigonometric results as a floating-point out-of-range error rather than NULL.
How do I compute COT(90 degrees) accurately?
Use ROUND(COT(RADIANS(90)), 6). Due to binary floating-point rounding on PI(), raw COT(RADIANS(90)) returns 6.12e-17 rather than exact 0.
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.