Java Math tanh() Method
The tanh() method of Java Math class returns the hyperbolic tangent of the specified double value.
Syntax:
public static double tanh(double x)
Parameters:
The parameter ‘x’ represents the number whose hyperbolic tangent is to be determined.
Return Value:
The tanh() method returns the hyperbolic tangent of the argument.
- It returns zero with same sign as argument, if the argument passed is zero.
- It returns +1.0, if the argument is positive infinity.
- It returns -1.0, if the argument is negative infinity.
- It returns NaN, if the argument is NaN.
Example 1:
public class JavaMathTanhExample1 {
public static void main(String[] args) {
double a=30;
//return the hyperbolic tangent value for a
System.out.println("Hyperbolic tangent value : "+Math.tanh(a));
}
}
Output:
Hyperbolic tangent value : 1.0
Example 2:
public class JavaMathTanhExample2 {
public static void main(String[] args) {
//returns +1.0, if the argument is positive infinity
double a=Double.POSITIVE_INFINITY;
System.out.println("Hyperbolic tangent value for "+a+" = "+Math.tanh(a));
}
}
Output:
Hyperbolic tangent value for Infinity = 1.0
Example 3:
public class JavaMathTanhExample3 {
public static void main(String[] args) {
//returns -1.0, if the argument is negative infinity
double a=Double.NEGATIVE_INFINITY;
System.out.println("Hyperbolic tangent value for "+a+" = "+Math.tanh(a));
}
}
Output:
Hyperbolic tangent value for -Infinity = -1.0
Example 4:
public class JavaMathTanhExample4 {
public static void main(String[] args) {
//returns zero with same sign as argument, if the argument passed is zero
double x = -0d;
System.out.println("Hyperbolic tangent value for "+x+" = "+Math.tanh(x));
}
}
Output:
Hyperbolic tangent value for -0.0 = -0.0
Example 5:
public class JavaMathTanhExample5 {
public static void main(String[] args) {
//return NaN, if the argument passed is NaN
double x = Double.NaN;
System.out.println("Hyperbolic tangent value for "+x+" = "+Math.tanh(x));
}
}
Output:
Hyperbolic tangent value for NaN = NaN
