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 July 2015.

La fonction Math.tanh() renvoie la tangente hyperbolique d'un nombre définie par :

tanhx=sinhxcoshx=ex-e-xex+e-x=e2x-1e2x+1\tanh x = \frac{\sinh x}{\cosh x} = \frac {e^x - e^{-x}} {e^x + e^{-x}} = \frac{e^{2x} - 1}{e^{2x}+1}

Exemple interactif

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

Syntaxe

js
Math.tanh(x);

Paramètres

x

Un nombre.

Valeur de retour

La tangente hyperbolique du nombre fourni en argument.

Description

tanh() est une méthode statique de l'objet Math, elle doit toujours être utilisée avec la syntaxe Math.tanh(), elle ne doit pas être utilisée comme une méthode d'un objet Math qui aurait été instancié (Math n'est pas une constructeur).

Exemples

Utiliser Math.tanh()

js
Math.tanh(0); // 0
Math.tanh(Infinity); // 1
Math.tanh(1); // 0.7615941559557649

Prothèse d'émulation (polyfill)

Cette méthode peut être émulée grâce à la fonction 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);
  };

et si on souhaite n'utiliser qu'un seul appel à Math.exp() :

js
Math.tanhx =
  Math.tanhx ||
  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);
    }
  };

Spécifications

Specification
ECMAScript® 2025 Language Specification
# sec-math.tanh

Compatibilité des navigateurs

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
tanh

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Voir aussi