Math.log1p()

Math.log1p(x)
Returns: number · Updated March 13, 2026 · Math
math logarithm precision

The Math.log1p() function returns the natural logarithm of 1 + x (that is, log_e(1 + x)). The key advantage of this function is numerical precision: when x is very small (close to zero), computing Math.log(1 + x) directly can suffer from floating-point precision loss, while Math.log1p(x) maintains accuracy. This function is particularly useful in financial calculations, scientific computing, and any scenario involving small increments.

Syntax

Math.log1p(x)

Examples

Basic usage

console.log(Math.log1p(0));
// 0

console.log(Math.log1p(1));
// 0.6931471805599453 (same as Math.log(2))

console.log(Math.log1p(Math.E - 1));
// 1

Small values — where log1p shines

const x = 1e-10;

console.log(Math.log(1 + x));
// 9.999999999995e-11 (with potential precision loss)

console.log(Math.log1p(x));
// 9.999999999995e-11 (more accurate)

// For very small values, the difference becomes significant
const tiny = 1e-15;

console.log(Math.log(1 + tiny));
// 0 (floats to 1 due to precision)

console.log(Math.log1p(tiny));
// 9.992...e-16 (still accurate)

Negative values

console.log(Math.log1p(-0.5));
// -0.6931471805599453

console.log(Math.log1p(-0.9));
// -2.302585092994046

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

Common Patterns

Compound interest with small rates

const effectiveRate = (annualRate, compounds) => {
  return Math.pow(1 + annualRate / compounds, compounds) - 1;
};

// For small rates, use log1p for accuracy
const continuousRate = (annualRate) => {
  return Math.exp(annualRate) - 1;
};

console.log(continuousRate(0.001));
// 0.0010005003335835344

console.log(continuousRate(0.0001));
// 0.0001000050003335833

Financial calculations with small increments

const calculateReturn = (initial, final) => {
  // More accurate for small percentage changes
  return Math.log1p((final - initial) / initial);
};

console.log(calculateReturn(1000, 1001));
// 0.000999500333...

console.log(calculateReturn(1000, 1000.5));
// 0.000499833...

Probability and information theory

const entropy = (probabilities) => {
  return probabilities.reduce((sum, p) => {
    if (p === 0) return sum;
    return sum - p * Math.log1p(p - 1);
  }, 0);
};

console.log(entropy([0.5, 0.5]));
// 0.6931471805599453

console.log(entropy([0.9, 0.1]));
// 0.3250829733914482

See Also