# Java tanh Function

The Java tanh Function is a Math function that 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 a 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 is zero, the Math.tanh function will return zero with the same sign.
• When the number argument is positive infinity, the Java Math.tanh function will return the result as +1.0.
• If it is negative infinity, 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 the tan Function Function article to understand the Java Tangent 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 example, First, We declared variable x of type Double. We used the 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 an equivalent radiant. Then the System.out.println statement prints the result as output.

```double m = Math.toRadians(45);
System.out.println("\nHyperbolic Tangent value of Negative Value = " + Math.tanh(m));```

## tanh function Array example

In this Java tanh function program, we find the Hyperbolic Tangent values of bulk data or arrays. Here, we are going to declare an array of double types and find the Hyperbolic tangent values of array elements using the 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]));
}
}
}```
``````Hyperbolic Tangent of Array Element 0.00 = 0.0000
Hyperbolic Tangent of Array Element 1.00 = 0.7616
Hyperbolic Tangent of Array Element -2.00 = -0.9640
Hyperbolic Tangent of Array Element 30.00 = 1.0000
Hyperbolic Tangent of Array Element -4.00 = -0.9993
Hyperbolic Tangent of Array Element 5.00 = 0.9999
Hyperbolic Tangent of Array Element -0.92 = -0.7259
Hyperbolic Tangent of Array Element -1.98 = -0.9626
Hyperbolic Tangent of Array Element -6.48 = -1.0000``````

We used the For Loop to iterate the Array. Within the Java tanh function For Loop, we initialized the i value as 0. Next, the 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));
}
}
}```
``````Hyperbolic Tangent of ArrayList Item 0.45 =  0.4219
Hyperbolic Tangent of ArrayList Item 2.49 =  0.9863
Hyperbolic Tangent of ArrayList Item -6.60 =  -1.0000
Hyperbolic Tangent of ArrayList Item -4.75 =  -0.9999
Hyperbolic Tangent of ArrayList Item 10.68 =  1.0000
Hyperbolic Tangent of ArrayList Item -10.68 =  -1.0000 ``````

First, We used the For Loop to iterate the double values in ArrayList

`for (double x : myList) {`

Here, the compiler will call the 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));`