Skip to content

Core\Math::atan

Returns the angle whose tangent is the number you give it. This is the arc tangent, and it undoes Core\Math::tan.

Any number works, because every number is the tangent of some angle. The angle is in radians, between -PI / 2 and PI / 2. An infinite number gives exactly PI / 2 or -PI / 2. Core\Math::toDegrees converts the angle to degrees.

Good to know: when you divide one coordinate by the other to get the tangent, the signs of the two coordinates are lost. The points (1, 1) and (-1, -1) then give the same angle. Use Core\Math::atan2 for a point, because it takes both coordinates.

The examples below show the angles for a few tangents, then very big tangents and the case where Core\Math::atan2 is the right choice, then the angle of a road from its gradient.

Parameters

  • $nfloatA tangent: any number.

Return value

Returns float. An angle between -PI / 2 and PI / 2, in radians. An infinite $n gives exactly PI / 2 or -PI / 2.

Examples

The angle for a tangent

<?nvs
// Shows the angles `Core\Math::atan` returns for a few tangents, in radians and in degrees.
// A tangent of 1.0 belongs to an angle of 45 degrees. This prints 0.78539816339745.
echo Core\Math::atan(1.0), "\n";
// `Core\Math::toDegrees` converts the angle to degrees. This prints 45.
echo Core\Math::toDegrees(Core\Math::atan(1.0)), "\n";
// A negative tangent gives a negative angle. This prints -45.
echo Core\Math::toDegrees(Core\Math::atan(-1.0)), "\n";
Output
0.78539816339745
45
-45

Big tangents and lost signs

<?nvs
// Shows the angles for very big tangents, and when to use `Core\Math::atan2` instead.
// A big tangent gives an angle close to 90 degrees. This prints 89.99994270422.
echo Core\Math::toDegrees(Core\Math::atan(1000000.0)), "\n";
// An infinite tangent gives exactly 90 degrees. This prints 90.
echo Core\Math::toDegrees(Core\Math::atan(Core\Math::INFINITY)), "\n";
// The point (-1, -1) is at -135 degrees. Dividing -1.0 by -1.0 gives 1.0 and loses
// both minus signs, so `Core\Math::atan` prints 45.
echo Core\Math::toDegrees(Core\Math::atan(-1.0 / -1.0)), "\n";
// `Core\Math::atan2` takes both coordinates, y first. This prints -135.
echo Core\Math::toDegrees(Core\Math::atan2(-1.0, -1.0)), "\n";
Output
89.99994270422
90
45
-135

The angle of a road

<?nvs
// Shows how a cycling app turns the gradient on a road sign into the angle of the road.
// A gradient of 8% means the road climbs 8 metres over 100 metres.
class Road
{
// Returns the angle of the road, in degrees, with one decimal place.
public static function angle(float $percent): float
{
// The climb divided by the distance is the tangent of the road's angle.
float $radians = Core\Math::atan($percent / 100.0);
return Core\Math::round(Core\Math::toDegrees($radians), { precision: 1 });
}
}
// This prints 4.6.
echo Road::angle(8.0), "\n";
// A steep mountain road. This prints 6.8.
echo Road::angle(12.0), "\n";
// A gradient of 100% is an angle of 45 degrees. This prints 45.
echo Road::angle(100.0), "\n";
Output
4.6
6.8
45