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()

class Main {
  public static void main(String[] args) {

    // create a double variable
    double value1 = 45.0;
    double value2 = 60.0;
    double value3 = 30.0;

    // convert into radians
    value1 = Math.toRadians(value1);
    value2 = Math.toRadians(value2);
    value3 = Math.toRadians(value3);

    // compute the hyperbolic tangent
    System.out.println(Math.tanh(value1));  // 0.6557942026326724
    System.out.println(Math.tanh(value2));  // 0.7807144353592677
    System.out.println(Math.tanh(value3));  // 0.4804727781564516
  }
}

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()

class Main {
  public static void main(String[] args) {

    // create a double variable
    double value1 = 45.0;
    double value2 = 60.0;
    double value3 = 30.0;

    // convert into radians
    value1 = Math.toRadians(value1);
    value2 = Math.toRadians(value2);
    value3 = Math.toRadians(value3);

    // compute the hyperbolic tangent: sinh()/cosh()
    // returns 0.6557942026326724
    System.out.println(Math.sinh(value1)/Math.cosh(value1));

    // returns 0.7807144353592677
    System.out.println(Math.sinh(value2)/Math.cosh(value2));

    // returns 0.4804727781564516
    System.out.println(Math.sinh(value3)/Math.cosh(value3));
  }
}

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

class Main {
  public static void main(String[] args) {

    // create a double variable
    double value1 = Double.POSITIVE_INFINITY;
    double value2 = Double.NEGATIVE_INFINITY;
    double value3 = Math.sqrt(-5);
    double value4 = 0.0;

    // convert into radians
    value1 = Math.toRadians(value1);
    value2 = Math.toRadians(value2);
    value3 = Math.toRadians(value3);
    value4 = Math.toRadians(value4);

    // compute the hyperbolic tangent
    System.out.println(Math.tanh(value1));  // 1.0
    System.out.println(Math.tanh(value2));  // -1.0
    System.out.println(Math.tanh(value3));  // NaN
    System.out.println(Math.tanh(value4));  // 0.0
  }
}

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.


Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges