Math.tanh() 函数将会返回一个数的双曲正切函数值,计算如下:
语法
Math.tanh(x)
参数
x- 待计算的数字
返回值
所给数字的双曲正切值。
描述
因为tanh()是Math的一个静态方法, 所以使用时应直接使用Math.tanh(),而不是作为用户创建的Math对象的方法(Math不是一个构造器)。
示例
使用 Math.tanh()
Math.tanh(0); // 0 Math.tanh(Infinity); // 1 Math.tanh(1); // 0.7615941559557649
Polyfill
tanh()函数可以用Math.exp()函数来模仿:
Math.tanh = Math.tanh || function(x) {
if (x === Infinity) {
return 1;
} else if (x === -Infinity) {
return -1;
} else {
return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));
}
}
只使用一次Math.exp():
Math.tanh = Math.tanh || function(x) {
if (x === Infinity) {
return 1;
} else if (x === -Infinity) {
return -1;
} else {
var y = Math.exp(2 * x);
return (y - 1) / (y + 1);
}
}
规范
| 规范 | 状态 | 注释 |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) Math.tanh |
Standard | 初始定义 |
| ECMAScript 2017 Draft (ECMA-262) Math.tanh |
Draft |
浏览器兼容性
| 特征 | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| 基础支持 | 38 | 25 (25) | 未实现 | 25 | 7.1 |
| 特征 | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| 基础支持 | 未实现 | 未实现 | 25.0 (25) | 未实现 | 未实现 | 8 |