Java Math tanh()

The hyperbolic tangent is equivalent to (ex - e-x)/(ex + e-x), where e is Euler's number. Also tanh = sinh/cosh.

The syntax of the tanh() method is:

Math.tanh(double value)

Here, tanh() is a static method. Hence, we are accessing the method using the class name, Math.


tanh() Parameters

The tanh() method takes a single parameter.

  • value - angle whose hyperbolic tangent is to be determined

Note: The value is generally used in radians.


tanh() Return Values

  • returns the hyperbolic tangent of value
  • returns NaN if the argument value is NaN
  • returns 1.0 if the argument is positive infinity
  • returns -1.0 if the argument is negative infinity

Note: If the argument is zero, then the method returns zero with the same sign as the argument.


Example 1: Java Math tanh()

In the above example, notice the expression,

Math.tanh(value1)

Here, we have directly used the class name to call the method. It is because tanh() is a static method.

Note: We have used the Java Math.toRadians() method to convert all the values into radians.


Example 2: Compute tanh() Using sinh() and cosh()

In the above example, notice the expression,

Math.sinh(value1)/Math.cosh(value2)

Here, we are computing the hyperbolic tangent using sinh()/cosh() formula. As we can see the result of tanh() and sinh()/cosh() is same.


Example 2: tanh() With Zero, NaN and Infinite

In the above example,

  • Double.POSITIVE_INFINITY - implements positive infinity in Java
  • Double.NEGATIVE_INFINITY - implements negative infinity in Java
  • Math.sqrt(-5) - square root of a negative number is not a number

We have used the Java Math.sqrt() method to calculate the square root of a number.

Note: The tanh() method returns 1.0 for the positive infinity argument and -1.0 for the negative infinity argument.


Recommended Tutorials