Java tanh 函数是 Math 函数,用于计算给定表达式的三角双曲正切值。
Java 中 Math.tanh 的基本语法如下所示。
static double tanh(double number); //Return Type is Double // In order to use in program: Math.tanh(double number);
Number: 它可以是数字或您想查找其双曲正切值的有效数值表达式。
- 如果 number 参数是正数或负数,Math.tanh 函数将返回双曲正切值。
- 如果 number 参数为零,Math.tanh 函数将返回相同符号的零。
- 当 number 参数为正无穷大时,Math.tanh 函数将返回 +1.0 作为结果。
- 如果为负无穷大,Math.tanh 函数将返回 -1.0 作为结果。
- 如果不是数字,Math.tanh 函数将返回 NaN。
Java tanh 函数示例
Java 中 x 的双曲正切可以定义为 (e^x – e^-x) / (e^x + e^-x),其中 E 是欧拉数。在此示例中,我们使用 Math.tanh 函数查找正值和负值的双曲正切值并显示输出。
提示:请参考 tan 函数 文章以了解 Java 正切函数。
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));
}
}

在此示例中,我们首先声明了一个 Double 类型的变量 x。我们直接在表达式上使用了该函数。这里,我们使用 System.out.println 语句将结果作为输出打印出来。
double x = Math.tanh(13.65 - 10.95 + 4.38);
System.out.println("Hyperbolic Tangent value = " + x);
接下来,我们直接对正双精度值使用了 Math.tanh 函数。
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));
接下来,我们直接对负双精度值使用了 Math.tanh 函数。
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 类型的变量并赋值。接下来,我们使用 Math.toRadians 函数将 45 转换为等效的弧度。然后 System.out.println 语句将结果作为输出打印出来。
double m = Math.toRadians(45);
System.out.println("\nHyperbolic Tangent value of Negative Value = " + Math.tanh(m));
tanh 函数数组示例
在此 Java tanh 函数程序中,我们查找批量数据或数组的双曲正切值。这里,我们将声明一个双精度类型的数组,并使用 tanh 函数查找数组元素的双曲正切值。
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
我们使用 For 循环 来迭代数组。在 tanh 函数 For 循环中,我们将 i 值初始化为 0。接下来,编译器将检查条件 (i < myArray.length)。
提示:myArray.length 用于查找数组的长度。
for (int i = 0; i < myArray.length; i++) {
在这里,我们在 System.out.format 语句中直接使用了 tanh 数学函数。这里,编译器将调用 Math.tanh 方法 (static double tanh(double number) ) 来查找相应的正切值并打印输出。
System.out.format("Hyperbolic Tangent of Array Element %.2f = %.4f\n", myArray[i], Math.tanh(myArray[i]));
注意:要查找单个项的双曲正切值,请使用:Math.tanh(myArray[index_position])
Java tanh 函数在 ArrayList 示例
在此 Java 程序中,我们声明了一个双精度类型的 ArrayList,并查找了列表中元素的双曲正切值。
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
首先,我们使用 For 循环迭代 ArrayList 中的双精度值
for (double x : myList) {
这里,编译器将调用 math tanh 方法( static double tanh(double x) )来查找相应的余弦值并打印输出。
System.out.format("Hyperbolic Tangent of ArrayList Item %.2f = %.4f \n", x, Math.tanh(x));