Java Math tanh()

tanh() accepts a double value as an argument and returns hyperbolic tangent of the argument. The returned value is of type double.

Following is the syntax of tanh() method.

</>
Copy
double value = tanh(double x)

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

We shall learn about some of the special cases for tanh() method with examples.

Example 1 – Math.tanh(double)

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

Java Program

</>
Copy
public class MathExample {
	public static void main(String[] args) {
		double x = 10;
		double result = Math.tanh(x);
		System.out.println(result);
	}
}

Output

0.9999999958776927

Example 2 – Math.tanh(int)

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

Java Program

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

Output

0.7615941559557649

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

Example 3 – Math.tanh(NaN)

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

Java Program

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

Output

NaN

Example 4 – Math.tanh() – With Positive Infinity as Argument

In the following example, we pass Double.POSITIVE_INFINITY as argument to tanh() method. As per the definition of the tanh() method in Math class, tanh() should return 1.0, for positive infinity.

Java Program

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

Output

1.0

Example 5 – Math.tanh() – With Negative Infinity as Argument

In the following example, we pass Double.POSITIVE_INFINITY as argument to tanh() method. As per the definition of the tanh() method in Math class, tanh() should return -1.0 for negative infinity value.

Java Program

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

Output

-1.0

Conclusion

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