Core\Math::tan
Returns the tangent of an angle. The angle is in radians, and Core\Math::toRadians converts degrees to radians. The tangent is the sine divided by the cosine, so it can be any number. An infinity or NaN (a value that means "not a number") gives NaN.
In plain words: the tangent says how steep a slope is. It is how far the slope goes up for each step of 1 forward.
Good to know: the result gets very large as the angle gets close to 90 degrees. A float cannot store 90 degrees in radians exactly, so the tangent of 90 degrees is a very large number. There is no error.
The examples below show the tangents of a few angles, then angles close to 90 degrees, then how a map app finds the grade of a road.
Parameters
$radiansfloatThe angle, in radians.Core\Math::toRadiansconverts degrees to radians.
Return value
Returns float. Any number. It is very large when the angle is close to 90 degrees. An infinity or NaN gives NaN.
Examples
Tangents of angles
<?nvs// Shows the tangents of a few angles in radians.
// The tangent of 0 is 0. This prints 0.echo Core\Math::tan(0.0), "\n";
// `Core\Math::PI / 4` radians is 45 degrees. Its tangent is 1, and this prints 1.echo Core\Math::tan(Core\Math::PI / 4.0), "\n";
// A negative angle gives the same result with the other sign.// These lines print 1.5574077246549 and -1.5574077246549.echo Core\Math::tan(1.0), "\n";echo Core\Math::tan(-1.0), "\n";011.5574077246549-1.5574077246549Close to 90 degrees
<?nvs// Shows how the tangent grows as the angle gets close to 90 degrees.
// At 45 degrees, a slope goes up 1 for each step of 1. This prints 1.echo Core\Math::tan(Core\Math::toRadians(45.0)), "\n";
// 89 degrees is very steep. This prints 57.289961630759.echo Core\Math::tan(Core\Math::toRadians(89.0)), "\n";
// 90 degrees gives a very large number, and no error. This prints 1.6331239353195E+16.echo Core\Math::tan(Core\Math::toRadians(90.0)), "\n";157.2899616307591.6331239353195E+16The grade of a road
<?nvs// Shows how a map app turns the angle of a road into the grade on a road sign.
class Road{ // The grade is how many meters the road goes up for each 100 meters forward. public static function grade(float $angleDegrees): float { float $percent = Core\Math::tan(Core\Math::toRadians($angleDegrees)) * 100.0; return Core\Math::round($percent, { precision: 1 }); }}
// A road at an angle of 3 degrees. This prints 5.2, so the sign says 5.2 %.echo Road::grade(3.0), "\n";
// A road at 5 degrees. This prints 8.7.echo Road::grade(5.0), "\n";
// A steep road at 10 degrees. This prints 17.6.echo Road::grade(10.0), "\n";5.28.717.6