cosh() Method in Java

Rumman Ansari   Software Engineer   2019-03-30 00:00:00   5992  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Description

On this tutorial, we will be showing a java example on how to use the cosh(double a) method of Math Class. The cosh(double x) returns the hyperbolic cosine of the method argument value. The following are special cases that must be take into consideration:

 

  • If the argument is NaN, then the result is NaN.
  • If the argument is infinite, then the result is positive infinity.
  • If the argument is zero, then the result is 1.0.

Most of the methods of the Math class is static and the cosh() method is no exception. Thus don’t forget that in order to call this method, you don’t have to create a new object instead call it using Math.cosh(a).

Method Syntax

public static double cosh(double x)

Method Argument

Data Type Parameter Description
double x The number whose hyperbolic cosine is to be returned.

Method Returns

The Math.cosh() method returns the hyperbolic cosine of the argument x.

Compatibility

Requires Java 1.5 and up

Example

Below is a java code demonstrates the use of cosh() method of Math class. The example presented might be simple however it shows the behavior of the cosh() method.



import java.util.Scanner;

/*
 * This example source code demonstrates the use of
 * cosh() method of Math class
 */

public class MathCoshExample {

	public static void main(String[] args) {

		// Ask for user input
		System.out.print("Enter an angle in degrees:");

		// use scanner to read the console input
		Scanner scan = new Scanner(System.in);

		// Assign the user to String variable
		String s = scan.nextLine();

		// close the scanner object
		scan.close();

		// convert the string input to double
		double value = Double.parseDouble(s);


		// get the hyperbolic cosine of the user input
		double coshValue = Math.cosh(value);
		System.out.println("Hyperbolic Cosine of " + s + " is " + coshValue);

	}

}

output

Below is the sample output when you run the above example.

Enter an angle in degrees:1.75
Hyperbolic Cosine of 1.75 is 2.9641883097280877
Press any key to continue . . .

The above java example source code demonstrates the use of cosh() method of Math class. We simply ask for user input and we use the Scanner class to parse it. Since we have used the nextLine() method to get the console value, and the return data type is String thus we have used the Double.parseDouble() to transform it into double. We have to convert it first to double because the cosh() method accepts double method argument.