The tan() function computes the tangent of an angle in radians—the ratio of opposite to adjacent on a right triangle, or sin/cos on the unit circle. Essential for slopes, rotations, and graphics.
01
Tangent
opp/adj.
02
math.h
Link -lm.
03
Radians
Not degrees.
04
tan(pi/4)=1
45°.
05
sin/cos
Identity.
06
vs tanh
Different.
Fundamentals
Definition and Usage
Tangent is opposite ÷ adjacent on a right triangle. Equivalently, tan(θ) = sin(θ) / cos(θ) when cos(θ) ≠ 0.
In C, tan(x) expects x in radians. At 45° (π/4 radians), opposite equals adjacent, so tan(M_PI / 4) is 1.0.
💡
Beginner Tip
Tangent blows up near 90° and 270° where cos is zero. To find an angle from a slope, use atan2(y, x) instead of dividing and inverting manually—it handles all quadrants correctly.
Pair with atan2() — recover angle from rise and run.
🧠 How tan() Works
1
Receive radians
Angle x in radians (convert from degrees if needed).
Input
2
Compute sin/cos
Internally: tangent = sine divided by cosine.
Compute
3
Return tan(x)
Unbounded double; huge near 90° multiples.
Output
=
🔢
Tangent value
Use atan2(y, x) to go from slope back to angle.
Important
📝 Notes
Input is radians, not degrees—convert with degrees * M_PI / 180.0.
tan(x) = sin(x) / cos(x) when cos(x) ≠ 0.
Undefined at π/2, 3π/2, … (90°, 270°, …).
Output is unbounded—not limited to [−1, 1] like sin/cos.
Do not confuse tan() with tanh() (hyperbolic).
Link with -lm on GCC/Clang Unix-like builds.
Performance
⚡ Optimization
tan() is implemented in the platform math library. If you already have sin and cos for the same angle, some libraries compute both together—but calling tan directly is fine unless profiling says otherwise. For angle-from-vector work, atan2 is the right inverse tool.
Wrap Up
Conclusion
tan() computes the tangent of an angle in radians—opposite over adjacent, or sin/cos. Convert degrees first, avoid angles where cosine is zero, and use atan2 to reverse the operation in 2D.
Continue with tanh() for hyperbolic tangent, or review atan2() for full-circle angle recovery.
tan(x) returns the tangent of an angle given in radians. On a right triangle, tangent is opposite divided by adjacent. For example, tan(pi/4) is 1.0 (45 degrees).
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double tan(double x).
Radians. Convert degrees first: radians = degrees * (M_PI / 180.0). tan(45) is not the same as tan(45 degrees).
Tangent is undefined when cos(x) is zero — at pi/2, 3*pi/2, and similar angles (90°, 270°, etc.). tan may return a very large value or infinity; avoid calling tan exactly at those angles.
tan(x) equals sin(x) / cos(x) when cos(x) is not zero. All three take radians. Use this identity to verify results or understand the function.
tan() is circular tangent (radians in, unbounded output with asymptotes). tanh() is hyperbolic tangent (any real input, output in (-1, 1)). Different functions despite similar names.
Did you know?
A road grade of 100% means a rise of 1 meter per 1 meter run—a 45° angle, because tan(45°) = 1. Steeper grades have higher tangent values; vertical cliffs approach infinity.