Base de connaissancesCentre d’examen
FRFrançais
Choisir la langue13

CKTIS / JavaScript

Math.log : logarithme naturel

Math.log convertit son argument en Number et renvoie une approximation du logarithme naturel de base e.

Version : ECMAScript 1 onward; example target: Node.js 24.22.2

Syntaxe

Math.log(x)

Math.log(x) applique ToNumber et calcule ln(x). Pour une valeur positive finie, le résultat peut être négatif, nul ou positif : log(1) vaut +0, log(Math.E) environ 1 et une valeur entre 0 et 1 donne un résultat négatif. log(+Infinity) vaut Infinity.

±0 donne -Infinity ; un nombre négatif, -Infinity ou NaN donne NaN. L’absence d’argument donne aussi NaN. Une chaîne numérique est convertie, Symbol ou BigInt lève TypeError et un objet peut exécuter valueOf ou toString. L’approximation dépend parfois de l’implémentation.

Pas un constructeur, standard depuis ECMAScript 1. Pour une croissance continue : Math.log(end / start) / time, avec des quantités positives. Pour ln(1+x) et un très petit x, utilisez Math.log1p(x) afin de limiter la perte de chiffres.

Exemples

const start = 100;
const end = 150;
const years = 5;
const continuousRate = Math.log(end / start) / years;
console.log(continuousRate.toFixed(6), (continuousRate * 100).toFixed(3));
console.log(Math.log(1), Math.log(Math.E));
console.log(Math.log(0), Number.isNaN(Math.log(-1)));
console.log(Number.isNaN(Math.log()));
try { Math.log(1n); } catch (error) { console.log(error instanceof TypeError); }

Résultat

0.081093 8.109
0 1
-Infinity true
true
true

Sources