Java Math tan() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Math

Definition

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.

Java
double flat = Math.tan(0.0);                       // 0.0
double steep = Math.tan(Math.toRadians(60));         // about 1.7321
double height = distance * Math.tan(angleInRadians); // rise from run
double percent = Math.tan(Math.toRadians(5)) * 100;  // road grade
CallWhat it does
Math.tan(0.0)Returns 0.0.
Math.tan(Math.PI / 4)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

ParameterDescription
aThe angle in radians. Smaller number types are widened to double.

Return Value

Return ValueDescription
doubleThe 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.

Compare with Similar Methods

Tangent and its neighbours:

MethodHow it differs
sin(a)Always between -1 and 1; the top of the fraction sin / cos.
cos(a)The bottom of the fraction; tan grows large where it nears zero.
atan(x)The inverse: finds the angle whose tangent is x.
atan2(y, x)Finds an angle from both sides, with the correct quadrant.

Java Examples

Example 1 — Tangent of 0, 45, and 60 Degrees

The tangent of a 45 degree angle should be exactly 1. See why it is not, and how to test it properly.

Java
import java.util.Locale;

public class MathTanBasics {
    public static void main(String[] args) {
        System.out.println("tan(0.0) = " + Math.tan(0.0));
        System.out.println("tan(PI / 4) = " + Math.tan(Math.PI / 4));
        System.out.println("rounded: " + String.format(Locale.US, "%.4f", Math.tan(Math.PI / 4)));
        double t45 = Math.tan(Math.toRadians(45));
        System.out.println("tan(45 degrees) == 1.0? " + (t45 == 1.0));
        System.out.println("within 1e-9 of 1.0? " + (Math.abs(t45 - 1.0) < 1e-9));
        System.out.println("tan(60 degrees) = " + String.format(Locale.US, "%.4f", Math.tan(Math.toRadians(60))));
    }
}

How It Works

  1. Zero stays zero. The flat slope at angle 0 has a tangent of exactly 0.0.
  2. 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.
  3. 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.

Java
import java.util.Locale;

public class MathTanAsymptote {
    public static void main(String[] args) {
        System.out.println("tan(PI / 2) = " + Math.tan(Math.PI / 2));
        System.out.println("infinite? " + Double.isInfinite(Math.tan(Math.PI / 2)));
        System.out.println("tan(PI) = " + Math.tan(Math.PI));
        System.out.println("89 degrees:   " + String.format(Locale.US, "%.2f", Math.tan(Math.toRadians(89))));
        System.out.println("89.9 degrees: " + String.format(Locale.US, "%.2f", Math.tan(Math.toRadians(89.9))));
        System.out.println("1.5 radians:  " + String.format(Locale.US, "%.2f", Math.tan(1.5)));
        System.out.println("1.6 radians:  " + String.format(Locale.US, "%.2f", Math.tan(1.6)));
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Java
import java.util.Locale;

public class MathTanHeight {
    public static void main(String[] args) {
        double distance = 20.0;
        double eyeHeight = 1.6;
        double height = eyeHeight + distance * Math.tan(Math.toRadians(35));
        System.out.println("Building height: " + String.format(Locale.US, "%.2f", height) + " m");

        double grade = Math.tan(Math.toRadians(5)) * 100;
        System.out.println("A 5 degree road climbs " + String.format(Locale.US, "%.2f", grade) + " m per 100 m");
        double steepest = Math.tan(Math.toRadians(45)) * 100;
        System.out.println("A 45 degree slope: " + String.format(Locale.US, "%.1f", steepest) + " percent");
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Next: tanh()

Hyperbolic tangent of a double, always between -1 and 1.

tanh() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful