C log1p() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

Definition

In C, log1p() computes log(1 + x) — the natural logarithm of one plus x. It is designed to stay accurate when x is very small, where writing log(1.0 + x) yourself can lose digits.

You pass one double. The argument must be greater than -1. Include <math.h>. On GCC, link with -lm. For a plain natural log, use log().

When x is tiny, 1.0 + x may round to 1.0 in floating-point math. log1p(x) avoids that trap and still returns a meaningful small result close to x.

Syntax

Pass the value that will be added to 1 inside the log.

C
log1p(1e-9);
CallWhat it does
log1p(1e-9)Returns a result very close to 1e-9, without losing precision.

Include <math.h>. On GCC, link with -lm. The domain is x > -1.

Library declarations
C
double log1p(double x);
float log1pf(float x);
long double log1pl(long double x);

Parameters

ParameterDescription
xRequired. The value added to 1 before taking the natural log. Must be greater than -1.

Return Value

Return ValueDescription
doubleNatural log of (1 + x). For tiny x, the result is near x itself.

C Examples

Example 1 — Basic log1p Call

Compute log(1 + x) for a few ordinary values.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    printf("%.6f\n", log1p(0.0));
    printf("%.6f\n", log1p(1.0));
    printf("%.6f\n", log1p(exp(1.0) - 1.0));
    return 0;
}

How It Works

  1. Zero input. log1p(0) is log(1), which is 0.
  2. Unit step. log1p(1) is log(2) ≈ 0.693147.
  3. e - 1 trick. log1p(e - 1) recovers 1.
  4. Natural log. The base is e, just like log().

Example 2 — Accuracy for Small x

Compare log1p with a naive log(1 + x) for a tiny value.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double x = 1e-16;
    printf("log1p   = %.17e\n", log1p(x));
    printf("log(1+x)= %.17e\n", log(1.0 + x));
    return 0;
}

How It Works

  1. Tiny x. 1e-16 is smaller than double epsilon around 1.
  2. Naive add fails. 1.0 + x may become exactly 1.
  3. log of 1 is 0. So the naive call loses the answer.
  4. log1p keeps it. It still reports a value near x.

Example 3 — Relative Growth

Turn a relative change into a log growth term with log1p.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double rate = 0.02; /* +2% */
    double g = log1p(rate);

    printf("log growth = %.6f\n", g);
    return 0;
}

How It Works

  1. Rate is relative. A 2% rise means multiply by 1.02.
  2. log1p(rate). That is log(1.02) without writing 1 + rate yourself.
  3. Slightly under rate. For small rates, log growth is a bit less than the percent.
  4. Useful in finance. Log returns often start from this idea.

Frequently Asked Questions

log1p(x) computes log(1 + x) accurately, even when x is very small.
x is required. It must be greater than -1.
A double equal to the natural logarithm of (1 + x).
Include <math.h>. On GCC, link with -lm.
For tiny x, 1.0 + x can round to 1.0 and erase the result. log1p avoids that.

Next: log2()

Compute the base-2 logarithm of a positive double.

log2() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful