Join our newsletter for the latest updates.

Java Math cosh()

The Java Math cosh() method returns the hyperbolic cosine of the specified value.

The hyperbolic cosine is equivalent to ( (ex + e-x)/2), where e is Euler's number.

The syntax of the cosh() method is:

Math.cosh(double value)

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


cosh() Parameters

The cosh() method takes a single parameter.

  • value - angle whose hyperbolic function is to be determined

Note: The value is generally used in radians.


cosh() Return Values

  • returns the hyperbolic cosine of value
  • returns NaN if the argument value is NaN
  • returns 1.0 if the argument is 0

Note: If the argument is infinity, then the method returns positive infinity.


Example 1: Java Math 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;
    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 cosine
    System.out.println(Math.cosh(value1));  // 1.3246090892520057
    System.out.println(Math.cosh(value2));  // 1.600286857702386
    System.out.println(Math.cosh(value3));  // 1.1402383210764286
    System.out.println(Math.cosh(value4));  // 1.0
  }
}

In the above example, notice the expression,

Math.cosh(value1)

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

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


Example 2: cosh() Returns 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);

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

    // compute the hyperbolic cosine
    System.out.println(Math.cosh(value1));  // Infinity
    System.out.println(Math.cosh(value2));  // Infinity
    System.out.println(Math.cosh(value3));  // NaN
  }
}

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 cosh() method returns positive infinity for both negative and positive infinity arguments.


Recommended Tutorials