What is Math.tan() in Java?

Java has a built-in Math class, defined in the java.lang package, which contains important functions to perform basic mathematical operations.

This class has the tan() method, which is used to compute a specified angle’s trigonometric tangent.

Method definition

public  static double tan(double angle);

Parameters

angle represents the double value corresponding to the angle in randians for which we have to find the trigonometric tangent.

Return value

The function returns the double value corresponding to the trigonometric tangent of the angle.

Note: Math.PI is a constant equivalent to π\pi in Mathematics.

If the value of the angle is infinity or NaNnot a number, then the returned value is NaN.

Note: To convert an angle measured in radians to an equivalent angle measured in degrees and vice versa, you can use the built-in Math.toDegrees(double angle_in_randians) and Math.toRadians(double angle_in_degrees) methods.

Code

class HelloWorld {
public static void main( String args[] ) {
double result = Math.tan(1);
System.out.println("tan(1) = " + result);
result = Math.tan(0);
System.out.println("tan(0) = " + result);
result = Math.tan(Math.toRadians(30));
System.out.println("tan(30°) = " + result);
result = Math.tan(Math.toRadians(45));
System.out.println("tan(45°) = " + result);
result = Math.tan(Math.toRadians(60));
System.out.println("tan(60°) = " + result);
result = Math.tan(Math.toRadians(90));
System.out.println("tan(90°) = " + result);
result = Math.tan(Double.NaN);
System.out.println("tan(NaN) = " + result);
result = Math.tan(Double.POSITIVE_INFINITY);
System.out.println("tan(∞) = " + result);
}
}

Free Resources