Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Java tanh Function

by suresh

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));
	}
}
Java tanh Function 1

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]));
		}
	}
}
Java tanh Function 2

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));
		}
	}
}
Java tanh Function 3

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));

Placed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy