In Java, the cosh() method is a part of the java.lang.Math class. This method is used to calculate the hyperbolic cosine of a given double value.
Mathematical Definition:
The hyperbolic cosine of any value a is defined as:
(ea + e-a)/2
where, e is Euler's number.
Special Cases: The cosh() method handles different cases, which are listed below,
- If the argument is NaN, then the result is NaN.
- If the argument is infinity then the result will be positive infinity.
- If the argument is zero, then the result is one.
These special cases make sure that the Math.cosh() methods work correctly.
Syntax of cosh() Method
public static double cosh(double a)
- Parameter: This method takes a single parameter a, which is the value whose hyperbolic cosine is to be returned.
- Return Type: This method returns the hyperbolic cosine value of the argument.
Now, we are going to discuss some exampls for better understanding.
Examples of Java Math cosh() Method
Example 1: In this example, we will see the basic usage of cosh() method.
// Java program to demonstrate working
// of Math.cosh() method
import java.lang.Math;
class Geeks {
public static void main(String args[]) {
double a = 3.5;
// Displaying hyperbolic cosine of 3.5
System.out.println(Math.cosh(a));
a = 90.328;
// Displaying hyperbolic cosine of 90.328
System.out.println(Math.cosh(a));
}
}
Output
16.572824671057315 8.470751974588509E38
Explanation: Here, we are calculating the hyperbolic cosine of given numbers. First, we are calculating the hyperbolic cosine of 3.5 and after that we are calculating the cosine of 90.328.
Example 2: In this example, we will see how cosh() method handles NaN and Infinity.
// Java program to demonstrate working
// Math.cosh() method with NaN and Infinity
import java.lang.Math;
public class Geeks {
public static void main(String[] args) {
double p = Double.POSITIVE_INFINITY;
double n = Double.NEGATIVE_INFINITY;
double nan = Double.NaN;
double res;
// here argument is negative infinity
res = Math.cosh(n);
System.out.println(res);
// here argument is positive infinity
res = Math.cosh(p);
System.out.println(res);
// here argument is NaN
res = Math.cosh(nan);
System.out.println(res);
}
}
Output
Infinity Infinity NaN