Python math.log1p() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math module

Definition

math.log1p(x) returns the natural logarithm of 1 + x, staying accurate when x is close to 0.

When x is tiny (like 1e-16), evaluating 1 + x first rounds away x to 1.0, making math.log(1 + x) return 0.0. log1p(x) avoids forming 1 + x.

math.log1p() is a function in Python's standard math module: import it with import math and call math.log1p(...), or import the name directly with from math import log1p.

Syntax

Import math and pass an increment x > -1.

python
import math

val = math.log1p(x)
CallWhat it does
math.log1p(0)Returns 0.0.
math.log1p(1e-16)Returns 1e-16 (where log(1 + 1e-16) returns 0.0).
math.log1p(math.e - 1)Returns 1.0.
math.log1p(-1)Raises ValueError: 1 + (-1) is zero.

Directly pairs with math.expm1() to convert cleanly between small relative increments and logarithmic scales.

Function signatures
python
math.log1p(x, /)
# returns: float ln(1 + x) for x > -1

Parameters

ParameterDescription
xRequired positional number strictly greater than -1.

Return Value

Return ValueDescription
floatNatural log of 1 + x. Returns inf for inf and nan for nan.

Python Examples

Example 1 — Convert Tiny Financial Returns Into Log Returns

Compare math.log(1 + r) and math.log1p(r) across percent and sub-epsilon changes.

python
import math

for r in [0.01, 0.0001, 1e-16]:
    naive = math.log(1.0 + r)
    accurate = math.log1p(r)
    print(f"r={r:.0e} naive={naive:.6e} log1p={accurate:.6e}")

How It Works

  1. Large return matches. At 1% and 0.01%, both expressions yield the same leading six digits.
  2. Precision threshold crossed. Adding 1e-16 to 1.0 loses the tiny increment in binary64, collapsing naive to zero.
  3. log1p stays intact. Evaluating around zero directly retains full 1.000000e-16 accuracy.

Example 2 — Cancel Upward and Downward Steps Symmetrically

Verify that forward gain x and inverse drop -x / (1 + x) sum to zero via math.log1p().

python
import math

x = 0.25
forward = math.log1p(x)
backward = math.log1p(-x / (1.0 + x))
print(f"forward={forward:.4f} backward={backward:.4f}")
print(math.isclose(forward + backward, 0.0, abs_tol=1e-15))

How It Works

  1. 25% upward move. Log return of +0.25 equals ln(1.25), displayed as 0.2231.
  2. Exact offsetting drop. Moving down by 0.25 / 1.25 = 0.20 yields ln(0.80), or -0.2231.
  3. Tolerance verification. math.isclose() confirms the sum equals zero within floating-point tolerance.

Example 3 — Inspect Values Near -1 and Domain Errors

Observe small negative increments and catch ValueError at and below -1.

python
import math

print(math.log1p(0))
print(f"{math.log1p(-0.5):.4f}")

for bad_val in [-1.0, -2.0]:
    try:
        math.log1p(bad_val)
    except ValueError:
        print(f"log1p({bad_val}) raises ValueError")

How It Works

  1. Zero increment. ln(1 + 0) returns exact 0.0.
  2. Negative fraction allowed. Subtracting 0.5 computes ln(0.5), shown as -0.6931.
  3. Boundary rejected. Both -1.0 and smaller values fall at or below zero inside the logarithm and raise ValueError.

Frequently Asked Questions

It stands for log(1 + x): the natural logarithm of one plus x.
When x is very small (such as 1e-16), 1.0 + x rounds to 1.0 and loses x completely. math.log1p(x) avoids that rounding loss.
Because 1 + (-1) is 0, and the logarithm of zero is not defined (negative infinity).
math.expm1(y) reverses math.log1p(x), recovering x cleanly near zero.
No. It always computes the natural logarithm (base e).

Next: math.log2()

Return the base-2 logarithm of x as a float.

log2() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful