Java Math.tan(): Trigonometric Tangent & Slope Calculations

The Math.tan() method returns the trigonometric tangent (sin / cos) of an angle measured in radians.

Unbounded Slope Ratio: Operates in radians; use Math.toRadians() for degree inputs. For sine and cosine components, see Math.sin() and Math.cos(). For inverse tangent, explore Math.atan(). For hyperbolic tangent, see Math.tanh().

1. Syntax & Method Signature 🔝

Defined in java.lang.Math:

 // Formal signature of Math.tan() in java.lang.Math
public static double tan(double a)
Signature Element Type Description & Operational Rule
a double The angle measured in radians (period π radians or 180°).
Return Value double The tangent slope ratio Opposite / Adjacent in (-infinity, +infinity).
Symmetry Odd Function Antisymmetric across origin: Math.tan(-a) == -Math.tan(a).

2. Standard Trigonometric Angles in Radians 🔝

Evaluating tangent across fundamental radian angles and verifying odd symmetry:

System.out.println(Math.tan(0.0));           // 0.0
System.out.println(Math.tan(Math.PI / 4));   // 0.9999999999999999 (~1.0, 45 deg)
System.out.println(Math.tan(-Math.PI / 4));  // -0.9999999999999999 (odd function)
System.out.println(Math.tan(1.22));          // 2.7327541993067146

Terminal Output:

0.0
0.9999999999999999
-0.9999999999999999
2.7327541993067146

3. Converting Degrees with Math.toRadians() 🔝

Always convert degree angles using Math.toRadians() (and optionally snap with Math.round()):

System.out.println(Math.tan(Math.toRadians(0)));    // 0.0
System.out.println(Math.round(Math.tan(Math.toRadians(45))));  // 1
System.out.println(Math.tan(Math.toRadians(60)));   // 1.7320508075688767 (~sqrt(3))

Terminal Output:

0.0
1
1.7320508075688767

4. The 90-Degree Asymptote Trap: Why Math.tan(Math.PI / 2) Is Not Infinity 🔝

Mathematically, tan(90°) is undefined (vertical asymptote). Because Math.PI / 2.0 is a finite 64-bit float that misses exact π/2 by 6.12e-17 radians, Math.tan(Math.PI / 2.0) returns the huge finite number 1.633123935319537E16:

 // THE 90-DEGREE ASYMPTOTE TRAP: Math.PI / 2 is slightly less than exact pi/2
double tan90  = Math.tan(Math.PI / 2.0);
double tan180 = Math.tan(Math.PI);

System.out.println("tan(PI/2): " + tan90);   // 1.633123935319537E16 (NOT Infinity!)
System.out.println("tan(PI):   " + tan180);  // -1.2246467991473532E-16 (NOT 0.0!)

Terminal Output:

tan(PI/2): 1.633123935319537E16
tan(PI):   -1.2246467991473532E-16

5. Special Floating-Point Values (-0.0, Infinity & NaN) 🔝

Signed zero is preserved, while infinite arguments return Double.NaN:

System.out.println(Math.tan(-0.0));                      // -0.0
System.out.println(Math.tan(Double.NaN));                // NaN
System.out.println(Math.tan(Double.POSITIVE_INFINITY));  // NaN

Terminal Output:

-0.0
NaN
NaN

6. Comparison: Java Trigonometric Function Family 🔝

Comparing Math.tan() with Math.sin(), Math.cos(), and Math.atan():

Method Ratio / Operation Return Range Periodicity
Math.tan(rad) Opposite / Adjacent (-infinity, +infinity) Period π (180°)
Math.sin(rad) Opposite / Hypotenuse [-1.0, 1.0] Period 2π (360°)
Math.cos(rad) Adjacent / Hypotenuse [-1.0, 1.0] Period 2π (360°)
Math.atan(slope) Inverse tangent angle (-pi/2, pi/2) rad Non-periodic inverse

7. Production Recipe: Surveying Clinometer Height Calculator 🔝

Computing the height of a building or tower from ground distance and angle of elevation:

public class ClinometerHeightCalculator {
    public static double buildingHeight(double distanceMeters, double elevationDeg, double eyeHeight) {
         // Opposite = Adjacent * tan(theta) + observer eye height
        return distanceMeters * Math.tan(Math.toRadians(elevationDeg)) + eyeHeight;
    }
    public static void main(String[] args) {
        double h = buildingHeight(50.0, 45.0, 1.7);
        System.out.println(String.format("Building Height: %.2f m", h));  // 51.70 m
    }
}

Terminal Output:

Building Height: 51.70 m

8. Practical Recipe: 3D Graphics Perspective Camera FOV Scale 🔝

Computing the perspective projection matrix focal-length scaling factor 1 / tan(fov / 2) in OpenGL/Vulkan engines:

public class CameraProjectionHelper {
    public static double focalLengthFromFov(double fovDegrees) {
         // Perspective projection scale: f = 1 / tan(fov / 2)
        double halfFovRad = Math.toRadians(fovDegrees) / 2.0;
        return 1.0 / Math.tan(halfFovRad);
    }
    public static void main(String[] args) {
        System.out.println(String.format("90 deg FOV scale: %.4f", focalLengthFromFov(90.0)));  // 1.0000
        System.out.println(String.format("60 deg FOV scale: %.4f", focalLengthFromFov(60.0)));  // 1.7321
    }
}

Terminal Output:

90 deg FOV scale: 1.0000
60 deg FOV scale: 1.7321

9. Common Developer Pitfalls & Traps 🔝

Checking Double.isInfinite(Math.tan(Math.toRadians(90)))

Because no binary floating-point number equals exact π/2, Math.tan(x) never returns Infinity for finite inputs. Guard against near-90° angles by checking Math.abs(Math.cos(rad)) < 1e-12.

Passing Unconverted Degrees

Calling Math.tan(45) evaluates 45 radians (1.619775), not 45 degrees (1.0). Always convert via Math.toRadians(45).

10. Complete Compilable Java Program 🔝

Compile and run this verification class in any Java environment:

public class MathTanFullDemo {
    public static void main(String[] args) {
         // 1. Verify tan(0.0) == 0.0
        if (Math.tan(0.0) == 0.0) {
            System.out.println("Check 1 Passed: tan(0.0) is 0.0.");
        }

         // 2. Verify odd symmetry: tan(-x) == -tan(x)
        if (Math.tan(-1.1) == -Math.tan(1.1)) {
            System.out.println("Check 2 Passed: Odd function symmetry verified.");
        }

         // 3. Verify quotient identity: tan(x) == sin(x) / cos(x)
        double x = 0.75;
        if (Math.abs(Math.tan(x) - (Math.sin(x) / Math.cos(x))) < 1e-12) {
            System.out.println("Check 3 Passed: sin(x)/cos(x) identity verified.");
        }

        System.out.println("All Math.tan() verification checks passed successfully!");
    }
}

Terminal Output:

Check 1 Passed: tan(0.0) is 0.0.
Check 2 Passed: Odd function symmetry verified.
Check 3 Passed: sin(x)/cos(x) identity verified.
All Math.tan() verification checks passed successfully!

11. Frequently Asked Questions (FAQ) 🔝

Why does Math.tan(Math.toRadians(45)) return 0.9999999999999999?

Because π/4 cannot be represented exactly in binary floating-point, the result is 1 ULP below 1.0.

How do I convert a tangent slope back into an angle?

Use Math.atan(slope) (or Math.atan2(y, x) for full 4-quadrant coordinates), then convert to degrees with Math.toDegrees().

How do I compute cotangent (cot) in Java?

Use 1.0 / Math.tan(rad) or Math.cos(rad) / Math.sin(rad).

What is the difference between Math.tan() and Math.tanh()?

Math.tan() is the periodic circular tangent (-infinity, +infinity), while Math.tanh() is the S-shaped hyperbolic tangent bounded to (-1.0, 1.0).

Further Reading & Related Tutorials 🔝


Java



plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer