Java Math cosh()
cosh() accepts a double value as an argument and returns hyperbolic cosine of the argument. The returned value is of type double.
Following is the syntax of cosh() method.
double value = cosh(double x)
Since the definition of cosh() 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 cosh() method with examples.
Example 1 – Math.cosh(double)
In the following example, we pass angle in radians as argument to cosh() method and find its value.
Java Program
public class MathExample {
public static void main(String[] args) {
double x = 10;
double result = Math.cosh(x);
System.out.println(result);
}
}
Output
11013.232920103324
Example 2 – Math.cosh(int)
In the following example, we pass an int value for the argument to cosh() method.
Java Program
public class MathExample {
public static void main(String[] args) {
int x = 5;
double result = Math.cosh(x);
System.out.println(result);
}
}
Output
74.20994852478785
Similarly, you can provide a float or long value as argument to cosh() method.
Example 3 – Math.cosh(NaN)
In the following example, we pass Double.NaN as argument to cosh() method. As per the definition of the sin() in Math class, the method should return NaN value.
Java Program
public class MathExample {
public static void main(String[] args) {
double x = Double.NaN;
double result = Math.cosh(x);
System.out.println(result);
}
}
Output
NaN
Example 4 – Math.cosh() – With Positive Infinity as Argument
In the following example, we pass Double.POSITIVE_INFINITY as argument to cosh() method. As per the definition of the cosh() method in Math class, cosh() should return Infinity value with opposite same sign as that of argument.
Java Program
public class MathExample {
public static void main(String[] args) {
double x = Double.POSITIVE_INFINITY;
double result = Math.cosh(x);
System.out.println(result);
}
}
Output
-Infinity
Example 5 – Math.cosh() – With Negative Infinity as Argument
In the following example, we pass Double.POSITIVE_INFINITY as argument to cosh() method. As per the definition of the cosh() method in Math class, cosh() should return Infinity value with opposite same sign as that of argument.
Java Program
public class MathExample {
public static void main(String[] args) {
double x = Double.NEGATIVE_INFINITY;
double result = Math.cosh(x);
System.out.println(result);
}
}
Output
Infinity
Conclusion
In this Java Tutorial, we learned about Java Math.cosh() function, with example programs.