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
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 Latest 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 |