In Java, Math.tan(a) returns the tangent of the angle a, given in radians. The tangent is the sine divided by the cosine, and it measures steepness: rise / run for a slope at angle a.
Unlike sine and cosine, the result has no upper or lower limit. It is used to find heights from a distance and an angle of elevation, to turn a slope angle into a gradient, and in camera and graphics code.
A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.tan(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass an angle in radians as a double. The tangent comes back as a double.
A 45 degree angle: returns 0.9999999999999999, not exactly 1.
Math.tan(Math.PI / 2)
Returns a huge number (about 1.6E16), not infinity.
Math.tan(Math.toRadians(35))
The slope of a 35 degree angle, about 0.70.
There is only a double version. The tangent repeats every Math.PI radians and has no finite value at odd multiples of PI / 2, where the mathematical answer would be infinite.
Library declarations
Java
// java.lang.Math
public static double tan(double a)
Parameters
Parameter
Description
a
The angle in radians. Smaller number types are widened to double.
Return Value
Return Value
Description
double
The tangent of a. The result is NaN if a is NaN or an infinity. For a zero argument the result is a zero with the same sign.
Zero stays zero. The flat slope at angle 0 has a tangent of exactly 0.0.
One ulp short of 1.Math.PI / 4 is a hair below a true eighth of a turn, so the tangent prints 0.9999999999999999, the double just under 1.0; %.4f rounds it back to 1.0000.
Exact comparison fails.Math.toRadians(45) equals Math.PI / 4 here, so == gives false while the tolerance test gives true; at 60 degrees the tangent is the square root of 3, 1.7321.
Example 2 — Huge, Not Infinite: The Tangent Near 90 Degrees
The tangent climbs without limit as the angle nears a quarter turn, and flips sign once it passes it.
Finite by accident.Math.PI / 2 is about 6E-17 short of a true quarter turn, so the cosine is a tiny positive number and the tangent is a huge finite one, 1.633123935319537E16.
It keeps climbing. From 89 to 89.9 degrees the tangent grows from 57.29 to 572.96, roughly ten times, as the cosine shrinks by the same factor.
The sign flips across 1.5708. 1.5 radians is just before the quarter turn (positive, 14.10) and 1.6 radians is just after it (negative, -34.23); at Math.PI itself the tangent is back near zero.
Example 3 — Measure a Building and Convert an Angle to a Slope
You stand 20 m from a building and see its top at an angle of 35 degrees. Your eyes are 1.6 m above the ground.
Building height: 15.60 m
A 5 degree road climbs 8.75 m per 100 m
A 45 degree slope: 100.0 percent
How It Works
Rise from run. The tangent of 35 degrees is about 0.7002, so 20 m of distance gives 14.00 m of height above eye level; adding 1.6 m of eye height gives 15.60 m.
Angle to road grade. The tangent of 5 degrees is about 0.0875, so multiplying by 100 shows the road rises 8.75 m for every 100 m travelled sideways.
45 degrees is 100 percent. Rise equals run at 45 degrees, so the tangent is (almost exactly) 1; the double is 0.9999999999999999 and the rounding to one decimal prints 100.0.
Frequently Asked Questions
It returns the tangent of an angle given in radians, as a double. For example, Math.tan(Math.toRadians(60)) is about 1.7321.
Because Math.PI / 4 is a double that sits a tiny bit below a true 45 degree angle. The tangent is the double just under 1.0. Round it or compare with a tolerance.
About 1.633123935319537E16, a huge finite number, not infinity and not an exception. Math.PI / 2 is not exactly 90 degrees, so the cosine is tiny but not zero.
Multiply the distance by the tangent of the angle of elevation: height = distance * Math.tan(Math.toRadians(angle)). Add your eye height if you measure from eye level.
Math.atan(x) returns the angle in radians between -PI/2 and PI/2. When you have both the rise and the run, Math.atan2(rise, run) is safer.