Math.log1p()

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.

The Math.log1p() static method returns the natural logarithm (base e) of 1 + x, where x is the argument. That is:

∀x>−1,𝙼𝚊𝚝𝚑.𝚕𝚘𝚐𝟷𝚙(𝚡)=ln(1+x)\forall x > -1,\;\mathtt{\operatorname{Math.log1p}(x)} = \ln(1 + x)

Try it

console.log(Math.log1p(1));
// Expected output: 0.6931471805599453

console.log(Math.log1p(0));
// Expected output: 0

console.log(Math.log1p(-1));
// Expected output: -Infinity

console.log(Math.log1p(-2));
// Expected output: NaN

Syntax

js
Math.log1p(x)

Parameters

x

A number greater than or equal to -1.

Return value

The natural logarithm (base e) of x + 1. If x is -1, returns -Infinity. If x < -1, returns NaN.

Description

For very small values of x, adding 1 can reduce or eliminate precision. The double floats used in JS give you about 15 digits of precision. 1 + 1e-15 = 1.000000000000001, but 1 + 1e-16 = 1.000000000000000 and therefore exactly 1.0 in that arithmetic, because digits past 15 are rounded off.

When you calculate log(1 + x), where x is a small positive number, you should get an answer very close to x because: limx→0log⁡(1+x)x=1\lim_{x \to 0} \frac{\log(1+x)}{x} = 1. If you calculate Math.log(1 + 1.1111111111e-15), you should get an answer close to 1.1111111111e-15. Instead, you will end up taking the logarithm of 1.00000000000000111022 (the roundoff is in binary, so sometimes it gets ugly), and get the answer 1.11022…e-15, with only 3 correct digits. If you calculate Math.log1p(1.1111111111e-15) instead, you will get a much more accurate answer, 1.1111111110999995e-15, with 15 correct digits of precision (actually 16 in this case).

If the value of x is less than -1, the return value is always NaN.

Because log1p() is a static method of Math, you always use it as Math.log1p(), rather than as a method of a Math object you created (Math is not a constructor).

Examples

Using Math.log1p()

js
Math.log1p(-2); // NaN
Math.log1p(-1); // -Infinity
Math.log1p(-0); // -0
Math.log1p(0); // 0
Math.log1p(1); // 0.6931471805599453
Math.log1p(Infinity); // Infinity

Specifications

Specification
ECMAScript® 2027 Language Specification
# sec-math.log1p

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
log1p
Chrome – Full support
Chrome 38 (Release date: 2014-10-07)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 25 (Release date: 2013-10-29)
footnote Full support
Opera – Full support
Opera 25 (Release date: 2014-10-15)
footnote Full support
Safari – Full support
Safari 8 (Release date: 2014-10-16)
footnote Full support
Chrome Android – Full support
Chrome Android 38 (Release date: 2014-10-08)
footnote Full support
Firefox for Android – Full support
Firefox for Android 25 (Release date: 2013-10-29)
footnote Full support
Opera Android – Full support
Opera Android 25 (Release date: 2014-10-16)
footnote Full support
Safari on iOS – Full support
Safari on iOS 8 (Release date: 2014-09-17)
footnote Full support
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – Full support
WebView Android 38 (Release date: 2014-10-08)
footnote Full support
WebView on iOS – Full support
WebView on iOS 8 (Release date: 2014-09-17)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 0.12 (Release date: 2015-02-06)
footnote Full support

Legend

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

Full support
Full support

See also