The Math.log1p()
static method returns the natural logarithm (base e) of 1 + x
, where x
is the argument. That is:
The Math.log1p()
static method returns the natural logarithm (base e) of 1 + x
, where x
is the argument. That is:
Math.log1p(x)
x
A number greater than or equal to -1.
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 . 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, instead, you calculate Math.log1p(1.1111111111e-15)
, 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).
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
Specification |
---|
ECMAScript Language Specification # sec-math.log1p |
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
log1p |
38 | 12 | 25 | 25 | 8 | 38 | 25 | 25 | 8 | 3.0 | 38 | 1.0 | 0.12.0 |
Β© 2005β2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log1p