Friday, September 5, 2025
HomeLanguagesJavaJava Math tanh() method with Examples

Java Math tanh() method with Examples

The java.lang.Math.tanh() returns the hyperbolic tangent of a double value passed to it as argument. The hyperbolic tangent of any value a is defined as,

((ea – e-a)/2)/((ea + e-a)/2)

where e is Euler’s number. In other words, we can say that tanh(a) = sinh(a)/cosh(a).

If the argument is NaN, then the result is NaN. If the argument is positive infinity then the result will be +1.0. If the argument is negative infinity then the result will be -1.0. If the argument is zero, then the result is zero with the same sign as that of the argument a.

Syntax :

public static double tanh(double a)
Parameter :
a : the value whose hyperbolic tangent is to be returned.

Return :
This method returns the hyperbolic tangent value of the argument.

Example 1 : To show working of java.lang.Math.tanh() method.




// Java program to demonstrate working
// of java.lang.Math.tanh() method
import java.lang.Math;
  
class Gfg {
  
    // driver code
    public static void main(String args[])
    {
  
        double a = 3.5;
  
        System.out.println(Math.tanh(a));
  
        a = 90.328;
  
        System.out.println(Math.tanh(a));
  
        a = 0;
        // argument is zero, output will also be 0
        System.out.println(Math.tanh(a));
    }
}


Output:

0.9981778976111987
1.0
0.0

Example 2 : To show working of java.lang.Math.tanh() method when argument is NaN or infinity.




// Java program to demonstrate working
// of java.lang.Math.tanh() method infinity case
import java.lang.Math; // importing java.lang package
  
public class GFG {
    public static void main(String[] args)
    {
  
        double positiveInfinity = 
               Double.POSITIVE_INFINITY;
        double negativeInfinity = 
               Double.NEGATIVE_INFINITY;
        double nan = Double.NaN;
        double result;
  
        // Here argument is negative infinity
        result = Math.tanh(negativeInfinity);
        System.out.println(result);
  
        // Here argument is positive infinity
        result = Math.tanh(positiveInfinity);
        System.out.println(result);
  
        // Here argument is NaN, output will be NaN
        result = Math.tanh(nan);
        System.out.println(result);
    }
}


Output:

-1.0
1.0
NaN
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS