Home » Java programming language

Java StrictMath tanh() Method with Example

StrictMath Class tanh() method: Here, we are going to learn about the tanh() method of StrictMath Class with its syntax and example.
Submitted by Preeti Jain, on December 27, 2019

StrictMath Class tanh() method

  • tanh() method is available in java.lang package.
  • tanh() method is used to return the hyperbolic tangent of an angle of the given parameter in the method or in other words, it returns [sinh(d)/cosh(d)]. Here, "tanh" stands for the hyperbolic tangent of an angle.
  • tanh() method is a static method so it is accessible with the class name and if we try to access the method with the class object then we will not get any error.
  • In this method, we pass only radians type argument (i.e. First we convert given argument in radians by using toRadians() method of StrictMath class then after we will pass the same variable in tanh() method).
  • tanh() method does not throw any exception at the time of returning hyperbolic tangent of the given angle.

Syntax:

    public static double tanh(double d);

Parameter(s):

  • double d – represents the value hyperbolic tangent is to be returned.

Return value:

The return type of this method is double – it returns the hyperbolic tangent of the given angle.

Note:

  • If we pass NaN, the method returns NaN.
  • If we pass an infinity (positive or negative), the method returns 1.0.
  • If we pass a zero (positive or negative), the method returns 0.0.

Example:

// Java program to demonstrate the example of 
// tanh(double d) method of StrictMath class.

public class Tanh {
    public static void main(String[] args) {
        // variable declarations
        double d1 = 7.0 / 0.0;
        double d2 = -7.0 / 0.0;
        double d3 = 0.0;
        double d4 = -0.0;
        double d5 = 60.0;

        // Display previous value of d1,d2,d3,d4 and d5
        System.out.println("d1: " + d1);
        System.out.println("d2: " + d2);
        System.out.println("d3: " + d3);
        System.out.println("d4: " + d4);
        System.out.println("d5: " + d5);

        // By using toRadians() method to convert absolute 
        // value into radians.
        d1 = StrictMath.toRadians(d1);
        d2 = StrictMath.toRadians(d2);
        d3 = StrictMath.toRadians(d3);
        d4 = StrictMath.toRadians(d4);
        d5 = StrictMath.toRadians(d5);

        // Here , we will get (1.0) because we are passing parameter 
        // whose value is (infinity)
        System.out.println("StrictMath.tanh (d1): " + StrictMath.tanh(d1));

        // Here , we will get (-1.0) because we are passing parameter 
        // whose value is (-infinity)
        System.out.println("StrictMath.tanh (d2): " + StrictMath.tanh(d2));

        // Here , we will get (0.0) because we are passing parameter
        // whose value is (0.0)
        System.out.println("StrictMath.tanh (d3): " + StrictMath.tanh(d3));

        // Here , we will get (-0.0) because we are passing parameter 
        // whose value is (-0.0)
        System.out.println("StrictMath.tanh (d4): " + StrictMath.tanh(d4));

        // Here we will find hyperbolic tangent of d5 by 
        // using tanh () method
        System.out.println("StrictMath.tanh (d5): " + StrictMath.tanh(d5));
    }
}

Output

d1: Infinity
d2: -Infinity
d3: 0.0
d4: -0.0
d5: 60.0
StrictMath.tanh (d1): 1.0
StrictMath.tanh (d2): -1.0
StrictMath.tanh (d3): 0.0
StrictMath.tanh (d4): -0.0
StrictMath.tanh (d5): 0.7807144353592677


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.