JavaScript Math tanh()

tanh() 方法计算指定数字的双曲正切值并返回它。

示例

// hyperbolic tangent of 1
let number = Math.tanh(1);
console.log(number); 

// Output: 0.7615941559557649

tanh() 语法

Math.tanh() 方法的语法是

Math.tanh(number)

这里,tanh() 是一个静态方法。因此,我们使用类名 Math 来访问该方法。


tanh() 参数

tanh() 方法接受一个参数

  • number - 需要计算其双曲正切的数字

tanh() 返回值

tanh() 方法返回

  • 给定参数 number 的双曲正切值
  • 对于非数字参数,返回 NaN(非数字)。

示例 1:JavaScript Math.tanh()

// hyperbolic tangent of negative number let number1 = Math.tanh(-1)
; console.log(number1);
// hyperbolic tangent of zero let number2 = Math.tanh(0);
console.log(number2);
// hyperbolic tangent of positive number let number3 = Math.tanh(2);
console.log(number3); // Output: // -0.7615941559557649 // 0 // 0.9640275800758169

在上面的示例中,Math.tanh() 方法计算了以下值的双曲正切:

  • -1(负数) - 结果为 -0.7615941559557649
  • 0(零) - 结果为 0
  • 2(正数) - 结果为 0.9640275800758169

注意: 数学上,双曲正切等于 (ex - e-x)/(ex + e-x)


示例 2:Math.tanh() 与无穷大值

// tanh() with negative infinity let number1 = Math.tanh(-Infinity);
console.log(number1); // Output: -1
// tanh() with positive infinity let number2 = Math.tanh(Infinity);
console.log(number2); // Output: 1

在这里,当 tanh() 方法与无穷值一起使用时,如果参数是 -Infinity,则返回 -1;如果参数是 Infinity,则返回 1


示例 3:Math.tanh() 与非数字参数

let string = "Harry";

// tanh() with string argument let value = Math.tanh(string);
console.log(value); // Output: NaN

在上面的示例中,我们尝试计算字符串 "Harry" 的双曲正切。因此,我们得到的输出是 NaN


另请阅读

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战