Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Math
  6. log1p()

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⁩.

TheMath.log1p() static method returns the natural logarithm (basee) of1 + x, wherex 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.6931471805599453console.log(Math.log1p(0));// Expected output: 0console.log(Math.log1p(-1));// Expected output: -Infinityconsole.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 (basee) ofx + 1. Ifx is -1, returns-Infinity. Ifx < -1, returnsNaN.

Description

For very small values ofx, 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), wherex is a small positive number, you should get an answer very close tox because:limx0log(1+x)x=1\lim_{x \to 0} \frac{\log(1+x)}{x} = 1. If you calculateMath.log(1 + 1.1111111111e-15), you should get an answer close to1.1111111111e-15. Instead, you will end up taking the logarithm of1.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 calculateMath.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 ofx is less than -1, the return value is alwaysNaN.

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

Examples

Using Math.log1p()

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

Specifications

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

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp