Home C Math Functions log1p() C log1p() Function 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.
Call What 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 double log1p(double x);
float log1pf(float x);
long double log1pl(long double x); Parameters Parameter Description x Required. The value added to 1 before taking the natural log. Must be greater than -1.
Return Value Return Value Description double Natural log of (1 + x). For tiny x, the result is near x itself.
C Examples 1. Basic log1p 2. Small x 3. Growth factor Example 1 — Basic log1p Call Compute log(1 + x) for a few ordinary values.
#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;
} 0.000000
0.693147
1.000000 How It Works Zero input. log1p(0) is log(1), which is 0.Unit step. log1p(1) is log(2) ≈ 0.693147.e - 1 trick. log1p(e - 1) recovers 1.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.
#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;
} log1p = 1.0000000000000000e-16
log(1+x)= 0.0000000000000000e+00 How It Works Tiny x. 1e-16 is smaller than double epsilon around 1.Naive add fails. 1.0 + x may become exactly 1.log of 1 is 0. So the naive call loses the answer.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.
#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 Rate is relative. A 2% rise means multiply by 1.02.log1p(rate). That is log(1.02) without writing 1 + rate yourself.Slightly under rate. For small rates, log growth is a bit less than the percent.Useful in finance. Log returns often start from this idea.Frequently Asked Questions What does log1p() do in C? log1p(x) computes log(1 + x) accurately, even when x is very small.
What is the parameter of log1p()? x is required. It must be greater than -1.
What does log1p() return? A double equal to the natural logarithm of (1 + x).
Which header file does log1p() need? Include <math.h>. On GCC, link with -lm.
Why not just write log(1 + x)? For tiny x, 1.0 + x can round to 1.0 and erase the result. log1p avoids that.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion