Math.log1p()
function returns the natural logarithm (base
e
) of 1 + a number, that is
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Math.log1p(x)
x
A number.
The natural logarithm (base
e
) of
1
plus the given number. If the number is less than
-1
,
NaN
被返回。
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), you should get an answer very close to x, if x is small (that's why these are called 'natural' logarithms). 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) , so you 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
.
因为
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).
This can be emulated with the following function:
Math.log1p = Math.log1p || function(x) {
x = Number(x);
if (x < -1 || x !== x)
return NaN;
if (x === 0 || x === Infinity)
return x;
var nearX = (x + 1) - 1;
return nearX === 0 ? x : x * (Math.log(x + 1) / nearX);
};
Math.log1p()
Math.log1p(1); // 0.6931471805599453 Math.log1p(0); // 0 Math.log1p(-1); // -Infinity Math.log1p(-2); // NaN
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Math.log1p' in that specification. |
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
log1p
|
Chrome 38 | Edge 12 | Firefox 25 | IE No | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 25 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
完整支持
不支持
Math
Math.abs()
Math.acos()
Math.acosh()
Math.asin()
Math.asinh()
Math.atan()
Math.atan2()
Math.atanh()
Math.cbrt()
Math.ceil()
Math.clz32()
Math.cos()
Math.cosh()
Math.exp()
Math.expm1()
Math.floor()
Math.fround()
Math.hypot()
Math.imul()
Math.log()
Math.log10()
Math.log1p()
Math.log2()
Math.max()
Math.min()
Math.pow()
Math.random()
Math.round()
Math.sign()
Math.sin()
Math.sinh()
Math.sqrt()
Math.tan()
Math.tanh()
Math.trunc()
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()