The Java tanh Function is a Java Math function, which calculates the trigonometric hyperbolic tangent for a given expression.
Java tanh Function syntax
The basic syntax of the Math.tanh in Java Programming language is as shown below.
static double tanh(double number); //Return Type is Double // In order to use in program: Math.tanh(double number);
Number: It can be a number or a valid numerical expression for which you want to find hyperbolic Tangent value.
- If the number argument is a positive or negative number, the Java Math.tanh function will return the hyperbolic tangent value.
- If the number argument zero, the Math.tanh function will return zero with the same sign.
- When the number argument is positive infinity, Math.tanh function will return the result as +1.0.
- If it is negative infinity, Java Math.tanh function will return the result as -1.0.
- If it is not a number, Math.tanh function will return NaN.
Java tanh Function Example
The Java hyperbolic tangent of x can be defined as (e^x – e^-x) / (e^x + e^-x), where is Euler’s Number E. In this example, We use Java Math.tanh Function to find the hyperbolic tangent values of both positive and negative values and display the output.
TIP: Please refer to Java tan Function Function article to understand the Java Tangent Function.
// Java Math.tanh Function package TrigonometricFunctions; public class TanhMethod { public static void main(String[] args) { double x = Math.tanh(13.65 - 10.95 + 4.38); System.out.println("Hyperbolic Tangent value = " + x); System.out.println("\nHyperbolic Tangent value of Positive Value = " + Math.tanh(14.25)); System.out.println("Hyperbolic Tangent value of Positive Value = " + Math.tanh(17.28)); System.out.println("\nHyperbolic Tangent value of Negative Value = " + Math.tanh(-24.85)); System.out.println("Hyperbolic Tangent value of Negative Value = " + Math.tanh(-10.95)); double m = Math.toRadians(45); System.out.println("\nHyperbolic Tangent value of Negative Value = " + Math.tanh(m)); } }
Within this Java tanh function example, First, We declared variable x of type Double. We used the Java Math.tanh function directly on expression. Here, we used System.out.println statement to print the result as output.
double x = Math.tanh(13.65 - 10.95 + 4.38); System.out.println("Hyperbolic Tangent value = " + x);
Next, We used the Math.tanh Function directly on Positive double values.
System.out.println("\nHyperbolic Tangent value of Positive Value = " + Math.tanh(14.25)); System.out.println("Hyperbolic Tangent value of Positive Value = " + Math.tanh(17.28));
Next, We used the Java Math.tanh Function directly on Negative double values.
System.out.println("\nHyperbolic Tangent value of Negative Value = " + Math.tanh(-24.85)); System.out.println("Hyperbolic Tangent value of Negative Value = " + Math.tanh(-10.95));
Here, We declared a variable of type Double and assigned the value. Next, we used the Math.toRadians function to convert 45 into equivalent radiant. Then the System.out.println statement to print the result as output.
double m = Math.toRadians(45); System.out.println("\nHyperbolic Tangent value of Negative Value = " + Math.tanh(m));
Java tanh on Array example
In this Java program, we find the Hyperbolic Tangent values of bulk data. Here, we are going to declare an array of double type and find the Hyperbolic tangent values of array elements using tanh function.
package TrigonometricFunctions; public class TanhMethodOnArrays { public static void main(String[] args) { double [] myArray = {0, 1, -2, 30, -4, 5, -0.920, -1.98, -6.48}; for (int i = 0; i < myArray.length; i++) { System.out.format("Hyperbolic Tangent of Array Element %.2f = %.4f\n", myArray[i], Math.tanh(myArray[i])); } } }
We used the For Loop to iterate the Array. Within the tanh For Loop, we initialized the i value as 0. Next, compiler will check for the condition (i < myArray.length).
TIP: myArray.length finds the length of an array.
for (int i = 0; i < myArray.length; i++) {
Here, we used the tanh Math function directly inside the System.out.format statement. Here, the compiler will call the Math.tanh method (static double tanh(double number) ) to find the corresponding Tangent values and prints the output.
System.out.format("Hyperbolic Tangent of Array Element %.2f = %.4f\n", myArray[i], Math.tanh(myArray[i]));
NOTE: To find the hyperbolic Tangent value of a single item, then use: Math.tanh(myArray[index_position])
Java tanh Function on Arraylist example
In this Java program, we declare an ArrayList of double type and find the hyperbolic Tangent values of list elements.
package TrigonometricFunctions; import java.util.ArrayList; public class TanhMethodOnArrayList { public static void main(String[] args) { ArrayList<Double> myList = new ArrayList<Double>(5); myList.add(0.45); myList.add(2.49); myList.add(-6.60); myList.add(-4.75); myList.add(10.68); myList.add(-10.68); for (double x : myList) { System.out.format("Hyperbolic Tangent of ArrayList Item %.2f = %.4f \n", x, Math.tanh(x)); } } }
First, We used the For Loop to iterate the double values in ArrayList
for (double x : myList) {
Here, the compiler will call math tanh method ( static double tanh(double x) ) to find the corresponding Cosine values and print the output.
System.out.format("Hyperbolic Tangent of ArrayList Item %.2f = %.4f \n", x, Math.tanh(x));