Java StrictMath tanh()方法与示例
StrictMath类tanh()方法
tanh()方法在java.lang包中可用。
tanh()方法用于返回方法中给定参数的角度的双曲正切值,换句话说,它返回[sinh(d)/cosh(d)]。在此,“tanh”代表某个角度的双曲正切。
tanh()方法是静态方法,因此可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会出现任何错误。
在此方法中,我们仅传递弧度类型的参数(即,首先,我们使用toRadians()StrictMath类的方法将给定的参数转换为弧度,然后在方法中传递相同的变量tanh())。
在返回给定角度的双曲正切时,tanh()方法不会引发任何异常。
语法:
public static double tanh(double d);
参数:
doubled–表示要返回的双曲正切值。
返回值:
此方法的返回类型为double-返回给定角度的双曲正切值。
注意:
如果传递NaN,则该方法返回NaN。
如果传递无穷大(正数或负数),则该方法返回1.0。
如果传递零(正数或负数),则该方法返回0.0。
示例
//Java程序演示的例子
//StrictMath类的tanh(doubled)方法。
public class Tanh {
public static void main(String[] args) {
//变量声明
double d1 = 7.0 / 0.0;
double d2 = -7.0 / 0.0;
double d3 = 0.0;
double d4 = -0.0;
double d5 = 60.0;
//显示d1,d2,d3,d4和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);
//通过使用toRadians()方法转换绝对值
//值变成弧度。
d1 = StrictMath.toRadians(d1);
d2 = StrictMath.toRadians(d2);
d3 = StrictMath.toRadians(d3);
d4 = StrictMath.toRadians(d4);
d5 = StrictMath.toRadians(d5);
//在这里,我们将得到(1.0),因为我们正在传递参数
//其值是(无穷大)
System.out.println("StrictMath.tanh (d1): " + StrictMath.tanh(d1));
//在这里,我们得到(-1.0),因为我们正在传递参数
//其值为(-infinity)
System.out.println("StrictMath.tanh (d2): " + StrictMath.tanh(d2));
//在这里,我们将得到(0.0),因为我们正在传递参数
//其值为(0.0)
System.out.println("StrictMath.tanh (d3): " + StrictMath.tanh(d3));
//在这里,我们将得到(-0.0),因为我们正在传递参数
//其值为(-0.0)
System.out.println("StrictMath.tanh (d4): " + StrictMath.tanh(d4));
//找到d5的双曲正切值
//使用tanh()方法
System.out.println("StrictMath.tanh (d5): " + StrictMath.tanh(d5));
}
}输出结果
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