Java cosh Function

The Java cosh Function is one of the Math functions to calculate the Trigonometric Hyperbolic Cosine for the specified expression.

Java cosh Function syntax

The basic syntax of the Math.cosh in Java Programming language is shown below.

static double cosh(double number); //Return Type is Double

// In order to use in program: 
Math.cosh(double number);

Number: It can be a number or a valid numerical expression for which you want to find a hyperbolic Cosine value.

  • If the number argument is a positive or negative number, the function will return the hyperbolic Cosine value.
  • If the number argument is zero, the Java Math.cosh function will return 1.0.
  • When the number argument is infinity, the function will return the result as Positive Infinity.
  • If it is not a number, the function will return NaN.

Java cosh Function Example

The Java hyperbolic cosine of x is (e^x + e^-x)/2, where is Euler’s Number E. In this example, we used the Java Math.cosh Function to find the hyperbolic Cosine values of both the positive and negative values and display the output.

TIP: Please refer to the Java cos Function article to understand the Java Cosine Function.

package TrigonometricFunctions;

public class CoshMethod {
	public static void main(String[] args) {
		double x = Math.cosh(1.25 - 6.75 + 9.68);
		System.out.println("Hyperbolic Cosine value =  " + x);	
		
		System.out.println("\nHyperbolic Cosine value of Positive Value = " + Math.cosh(10.25));
		System.out.println("Hyperbolic Cosine value of Positive Value =  " + Math.cosh(2.28));
		
		System.out.println("\nHyperbolic Cosine value of Negative Value =  " + Math.cosh(-4.85));
		System.out.println("Hyperbolic Cosine value of Negative Value =  " + Math.cosh(-7.95));

		double m = Math.toRadians(90);
		System.out.println("\nHyperbolic Cosine value of Negative Value = " + Math.cosh(m));
	}
}
Java cosh Function 1

First, We declared variable x of type Double and used the Java Math.cosh function directly on the expression. Here, we used System.out.println statement to print the result as output.

double x = Math.cosh(1.25 - 6.75 + 9.68);
System.out.println("Hyperbolic Cosine value =  " + x);

Next, We used the cosh Function directly on Positive double values.

System.out.println("\nHyperbolic Cosine value of Positive Value = " + Math.cosh(10.25));
System.out.println("Hyperbolic Cosine value of Positive Value =  " + Math.cosh(2.28));

Next, We used the Java Math.cosh Function directly on Negative double values.

System.out.println("\nHyperbolic Cosine value of Negative Value =  " + Math.cosh(-4.85));
System.out.println("Hyperbolic Cosine value of Negative Value =  " + Math.cosh(-7.95));

Here, We declared a variable of type Double and assigned the value. Next, we used the Math.toRadians function to convert 90 into an equivalent radiant. Then the System.out.println statement prints the result as output.

double m = Math.toRadians(90);
System.out.println("\nHyperbolic Cosine value of Negative Value = " + Math.cosh(m));

Java cosh on Array example

In this Java program, we find the Hyperbolic Cosine values of bulk data. Here, we will declare an array of double type and uses the Java cosh function to find the Hyperbolic Cosine values of array elements.

package TrigonometricFunctions;

public class CoshMethodOnArrays {
	public static void main(String[] args) {
		
		double [] myArray = {0, 1, 0.30, 0.45, 0.60, 0.75, 0.90, 0.120, 0.180, 10};

		for (int i = 0; i < myArray.length; i++) {
			System.out.format("Hyperbolic Cosine value of Array Element %.2f = %.4f\n", myArray[i], Math.cosh(myArray[i]));
		}
	}
}
Hyperbolic Cosine value of Array Element 0.00 = 1.0000
Hyperbolic Cosine value of Array Element 1.00 = 1.5431
Hyperbolic Cosine value of Array Element 0.30 = 1.0453
Hyperbolic Cosine value of Array Element 0.45 = 1.1030
Hyperbolic Cosine value of Array Element 0.60 = 1.1855
Hyperbolic Cosine value of Array Element 0.75 = 1.2947
Hyperbolic Cosine value of Array Element 0.90 = 1.4331
Hyperbolic Cosine value of Array Element 0.12 = 1.0072
Hyperbolic Cosine value of Array Element 0.18 = 1.0162
Hyperbolic Cosine value of Array Element 10.00 = 11013.2329

We used Java For Loop to iterate the Array. Within the cosh For Loop, we initialized the i value as 0. Next, the compiler will check for the condition (i < myArray.length).

TIP: myArray.length is used to find the length of an array.

for (int i = 0; i < myArray.length; i++) {

Here, we used the cosh Math function directly inside the System.out.format statement. The compiler will call the Math method (static double cosh(double number) ) to find the corresponding Cosine values and prints the output.

System.out.format("Hyperbolic Cosine value of Array Element %.2f = %.4f\n", myArray[i], Math.cosh(myArray[i]));

NOTE: To find the hyperbolic Cosine value of a single item, then use: Math.cosh(myArray[index_position])

Java cosh Function on Arraylist example

In this Java program, we declare an Arraylist of double type and find the hyperbolic Cosine values of list elements.

package TrigonometricFunctions;

import java.util.ArrayList;

public class CoshMethodOnArrayList {
	public static void main(String[] args) {
		
		ArrayList<Double> myList = new ArrayList<Double>(5);
		myList.add(5.45);
		myList.add(2.49);
		myList.add(-8.60);
		myList.add(-4.75);
		myList.add(10.68);
		myList.add(-10.68);
		
		for (double x : myList) {
			System.out.format("Hyperbolic Cosine value of ArrayList Item %.2f =  %.4f \n", x, Math.cosh(x));
		}
	}
}
Hyperbolic Cosine value of ArrayList Item 5.45 =  116.3812 
Hyperbolic Cosine value of ArrayList Item 2.49 =  6.0721 
Hyperbolic Cosine value of ArrayList Item -8.60 =  2715.8299 
Hyperbolic Cosine value of ArrayList Item -4.75 =  57.7965 
Hyperbolic Cosine value of ArrayList Item 10.68 =  21738.7752 
Hyperbolic Cosine value of ArrayList Item -10.68 =  21738.7752 

We used the For Loop to iterate the double values in ArrayList.

for (double x : myList) {

The Javac will call the math cosh method ( static double cosh(double x) ) to find the corresponding Cosine values and print the output.

System.out.format("Hyperbolic Cosine value of ArrayList Item %.2f =  %.4f \n", x, Math.cosh(x));