Signature
tan(float $num): floatPurpose
tan() returns the trigonometric tangent of an angle. The tangent is the ratio of the sine to the cosine of the same angle. In a right triangle it is the length of the side opposite the angle divided by the length of the adjacent side. That makes it useful for slopes, heights computed from a distance and an angle of elevation, simple geometry, and graphics code.
The angle must be given in radians, not degrees. This is the most frequent source of wrong results with this function.
Signature
Parameters and defaults
| Parameter | Meaning | Default |
|---|---|---|
$num |
The angle in radians | none, required |
There are no optional parameters. An integer argument is accepted and converted to a float. In the default non-strict mode a numeric string such as "0.5" is coerced as well. A non-numeric string throws a TypeError. With declare(strict_types=1) only int and float values are accepted.
Return value and failure cases
The function always returns a float. It does not return false and does not raise warnings for unusual numbers. Some inputs give special float results:
tan(0)returns0.0.tan(INF)andtan(-INF)returnNAN, because the tangent of an infinite angle is undefined.tan(NAN)returnsNAN.- Angles near odd multiples of π/2 produce very large positive or negative numbers, because the tangent has vertical asymptotes there.
Because the result is a floating-point number, expect tiny rounding errors. tan(M_PI_4) is mathematically exactly 1, but the computed value is a hair below it. Use round() for display, or compare with a tolerance instead of ==.
Example 1: the basic call
The tangent of π/4 radians (45 degrees) is 1. Rounding hides the last-digit noise.
echo round(tan(M_PI_4), 10), "\n";1
The constant M_PI_4 is π divided by four, so it is already in radians.
Example 2: converting degrees
Users usually think in degrees. Convert them with deg2rad() before calling tan().
foreach ([0, 30, 45, 60] as $degrees) {
$value = tan(deg2rad($degrees));
echo $degrees . ": " . round($value, 4) . "\n";
}0: 0 30: 0.5774 45: 1 60: 1.7321
The loop prints a small lookup table. Each value is rounded to four decimals so the output is stable and easy to read.
Example 3: height from distance and angle, plus edge cases
If you stand 50 metres from a tower and measure an elevation angle of 30 degrees, the height above eye level is distance * tan(angle). The same script shows two special situations.
$distance = 50.0;
$angle = deg2rad(30);
echo round($distance * tan($angle), 2), "\n";
echo var_export(is_nan(tan(INF)), true), "\n";
echo var_export(abs(tan(M_PI_2)) > 1e15, true), "\n";28.87 true true
The first line is the practical calculation. The second confirms that an infinite input gives NAN, which you test with is_nan() because NAN == NAN is false. The third shows that tan(M_PI_2) is an enormous finite number rather than infinity. M_PI_2 is only the closest float to π/2, not the exact value, so the true asymptote is never hit exactly.
Edge cases
- Asymptotes: results explode near 90 degrees, 270 degrees and so on. Small changes of the input cause huge changes of the output, so never rely on an exact value there. If your code can approach these angles, check
cos()first or limit the allowed range. - Periodicity: the tangent repeats every π radians, so
tan($x)andtan($x + M_PI)are equal up to rounding. Very large inputs lose precision because float angles become coarse. - Negative angles:
tan(-$x)equals-tan($x). - Negative zero:
tan(-0.0)returns-0.0. It compares equal to0.0but may show a sign in some outputs. - Slope versus angle:
tan()converts an angle into a slope. To go the other way, useatan(). For a full-circle direction from x and y components,atan2()is safer because it knows the quadrant.
Common mistakes
- Passing degrees directly.
tan(45)is not 1; it treats 45 as radians and returns roughly 1.62. Always wrap degree values indeg2rad(). - Comparing with
==.tan(M_PI_4) == 1is false because of rounding. Compare with a tolerance such asabs($a - $b) < 1e-9. - Rounding too early. Round only for output. Rounding intermediate values accumulates error.
- Expecting an error at 90 degrees. PHP does not throw or return
INFthere. You get a very large number, which may silently corrupt later calculations. - Testing NAN with equality. Use
is_nan(). - Feeding non-numeric strings. Validate user input first, or you will get a
TypeError.
Related functions
atan()returns the arc tangent, the inverse oftan().atan2()returns the arc tangent of two variables and respects quadrants.sin()andcos()are the sine and cosine;tan($x)is mathematicallysin($x) / cos($x).tanh()is the hyperbolic tangent, a different function despite the similar name.deg2rad()converts degrees to radians.
Recap
tan(float $num): float takes an angle in radians and returns its tangent as a float. Convert degrees with deg2rad(), round results only for display, expect NAN for infinite input, and remember that values near odd multiples of π/2 become extremely large.
Tested examples
echo round(tan(M_PI_4), 10), "\n";
1
foreach ([0, 30, 45, 60] as $degrees) {
$value = tan(deg2rad($degrees));
echo $degrees . ": " . round($value, 4) . "\n";
}
0: 0 30: 0.5774 45: 1 60: 1.7321
$distance = 50.0;
$angle = deg2rad(30);
echo round($distance * tan($angle), 2), "\n";
echo var_export(is_nan(tan(INF)), true), "\n";
echo var_export(abs(tan(M_PI_2)) > 1e15, true), "\n";
28.87 true true