Math.tanh()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
Math.tanh() 函数将会返回一个数的双曲正切函数值,公式如下:
尝试一下
console.log(Math.tanh(-1));
// Expected output: -0.7615941559557649
console.log(Math.tanh(0));
// Expected output: 0
console.log(Math.tanh(Infinity));
// Expected output: 1
console.log(Math.tanh(1));
// Expected output: 0.7615941559557649
语法
Math.tanh(x)
参数
x-
待计算的数字。
返回值
所给数字的双曲正切值。
描述
因为 tanh() 是 Math 的静态方法,所以总应该直接调用 Math.tanh() ,而不是创建 Math 对象再调用该方法(Math 不是一个构造函数)。
示例
>使用 Math.tanh()
js
Math.tanh(0); // 0
Math.tanh(Infinity); // 1
Math.tanh(1); // 0.7615941559557649
向下兼容
tanh() 可以通过 Math.exp() 函数实现:
js
Math.tanh =
Math.tanh ||
function (x) {
var a = Math.exp(+x),
b = Math.exp(-x);
return a == Infinity ? 1 : b == Infinity ? -1 : (a - b) / (a + b);
};
规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-math.tanh> |
浏览器兼容性
Loading…