The Math.tanh()
function returns the hyperbolic tangent of a number, that is
Math.tanh(x)
x
Because tanh()
is a static method of Math
, you always use it as Math.tanh()
, rather than as a method of a Math
object you created (Math
is not a constructor).
Math.tanh()
Math.tanh(0); // 0 Math.tanh(Infinity); // 1 Math.tanh(1); // 0.7615941559557649
This can be emulated with the help of the Math.exp() function:
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)); } }
or using only one call to 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); } }
Created by Mozilla Contributors, license: CC-BY-SA 2.5