Java Math tan()

tan() accepts angle in radians as an argument and returns tangent of the argument value. The returned value is of type double.

If the argument is NaN or infinity, then tan() returns NaN.

Following is the syntax of tan() method.

</>
Copy
double value = tan(double angle)

Since the definition of tan() function has double datatype as argument, you can pass int, float or long as arguments as these datatypes will implicitly promote to double.

Example 1 – Math.tan(double)

In the following example, we pass angle in radians as argument to tan() method and find its value.

Java Program

</>
Copy
public class MathExample {
	public static void main(String[] args) {
		double angle = 0.5; //radians
		double result = Math.tan(angle);
		System.out.println(result);
	}
}

Output

0.5463024898437905

0.5 radians is approximately equal to 28.6 degrees. And tan(28.6 degrees) is equal to 0.54 approximately. Our program gives a precise result.

Example 2 – Math.tan(int)

In the following example, we pass an int value for the argument to tan() method.

Java Program

</>
Copy
public class MathExample {
	public static void main(String[] args) {
		int angle = 2; //radians
		double result = Math.tan(angle);
		System.out.println(result);
	}
}

Output

-2.185039863261519

Similarly, you can provide a float or long value as argument to tan() method.

Example 3 – Math.tan(NaN)

In the following example, we pass Double.NaN as argument to tan() method. As per the definition of the function, tan() should return NaN value.

Java Program

</>
Copy
public class MathExample {
	public static void main(String[] args) {
		double angle = Double.NaN;
		double result = Math.tan(angle);
		System.out.println(result);
	}
}

Output

NaN

Example 4 – Math.tan() – With Infinity as Argument

In the following example, we pass Double.POSITIVE_INFINITY as argument to tan() method. As per the definition of the tan() method in Math class, tan() should return NaN value.

Java Program

</>
Copy
public class MathExample {
	public static void main(String[] args) {
		double angle = Double.POSITIVE_INFINITY;
		double result = Math.tan(angle);
		System.out.println(result);
	}
}

Output

NaN

Conclusion

In this Java Tutorial, we learned about Java Math.tan() function, with example programs.