Library contents

Java Math.tan

In Java, Math.tan takes an angle in radians and returns its tangent. Where it is defined, tangent is sine divided by cosine.

A simple example

Math.tan

  1. Start with 0 radians
  2. Do this Find rise divided by run
  3. Result 0
A unit line pointing horizontally has rise zero and run one. Dividing zero by one gives zero.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Math.tan(0.0));
    }
}
Output
0.0

Syntax

Java syntax
Math.tan(angle)
JDK 21 method signatures (1)
public static double tan(double a)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

Calculate rise per metre of horizontal run

A surveyor has the ramp angle and wants to know the rise for each horizontal metre. Express that relationship as a ratio the drawing team can apply to different ramp lengths.

From the programdouble angle = 0.7853981633974483
Result1.000
Main.java

class Main {
    public static void main(String[] args) throws Exception {
        double angle = 0.7853981633974483;

        System.out.printf(java.util.Locale.ROOT, "%.3f%n", (double) (Math.tan(angle)));
    }
}
Output
1.000

At 45 degrees the ramp rises one metre for every horizontal metre.

Advertisements

Check that a platform is level

A surveyor describes a platform or ramp using its rise and horizontal run. The drawing tool must convert between slope and angle, including the level case where the rise is zero.

From the programdouble angle = 0.0
Result0.000
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        double angle = 0.0;

        System.out.printf(java.util.Locale.ROOT, "%.3f%n", (double) (Math.tan(angle)));
    }
}
Output
0.000

A level platform has zero rise per unit of horizontal run.

Draw a sloping exhibit roof

A model roof rises at 30° across a 3 m horizontal run. A student needs the vertical rise to place the far edge in the drawing.

GivenRun 3 m · angle 30°
Worked result1.73 m

Multiply horizontal run by tangent to get rise.

What to watch for

Tangent becomes very large near a right angle. Use measured rise and run for a near-vertical slope.

Documentation for Math.tan