JavaScript Math cosh()

cosh() 方法计算给定数字的双曲余弦值并返回它。

示例

// hyperbolic cosine of 1
let number = Math.cosh(1);
console.log(number);

// Output: 1.5430806348152437

cosh() 语法

Math.cosh() 方法的语法是

Math.cosh(number)

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


cosh() 参数

cosh() 方法接受一个参数

  • number - 需要计算其双曲余弦的数字

cosh() 返回值

cosh() 方法返回

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

示例 1: JavaScript Math.cosh()

// hyperbolic cosine of negative number let number1 = Math.cosh(-1);
console.log(number1);
// hyperbolic cosine of zero let number2 = Math.cosh(0);
console.log(number2);
// hyperbolic cosine of positive number let number3 = Math.cosh(2);
console.log(number3); // Output: // 1.5430806348152437 // 1 // 3.7621956910836314

在上面的示例中,Math.cosh() 方法计算了以下数字的双曲余弦值:

  • -1 (负数) - 结果为 1.5430806348152437
  • 0 (零) - 结果为 1
  • 2 (正数) - 结果为 3.7621956910836314

注意: 数学上,双曲余弦等同于 (ex + e-x)/2


示例 2: Math.cosh() 结合 Infinity 值

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

示例 3: Math.cosh() 结合非数字参数

let string = "Harry";

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

在上面的示例中,我们尝试计算字符串 "Harry" 的双曲余弦值。这就是为什么我们得到 NaN 作为输出。


另请阅读

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

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

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

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