C expm1() Function

Definition
In C, expm1() returns exp(x) - 1. It stays accurate when x is near 0, where exp(x) is near 1.
You pass one double. The result is a double. Include <math.h>. On GCC, link with -lm.
Syntax
Pass the exponent. Start with this call.
| Call | What it does |
|---|
expm1(0.0) | Returns 0. |
expm1(1e-9) | Returns a value near 1e-9, without losing digits. |
Include <math.h>. On GCC, link with -lm. Prefer expm1(x) over exp(x) - 1 for tiny x.
Library declarations
double expm1(double x);
float expm1f(float x);
long double expm1l(long double x);
Parameters
| Parameter | Description |
|---|
| x | Required. The exponent used inside exp(x) - 1. |
Return Value
| Return Value | Description |
|---|
| double | exp(x) - 1. For small x, more accurate than writing exp(x) - 1 yourself. |
C Examples
Example 1 — Basic expm1 Values
Print expm1 at 0 and 1.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("%.6f\n", expm1(0.0));
printf("%.6f\n", expm1(1.0));
return 0;
}
How It Works
- Include math.h. That declares
expm1. - Zero input.
expm1(0) is 0. - Unit input.
expm1(1) is e − 1 ≈ 1.718. - Same idea as exp. Just shifted by subtracting 1 carefully.
Example 2 — Tiny x Accuracy
Compare exp(x) - 1 with expm1(x) for a very small x.
#include <stdio.h>
#include <math.h>
int main(void) {
double x = 1e-12;
printf("exp-1 = %.12e\n", exp(x) - 1.0);
printf("expm1 = %.12e\n", expm1(x));
return 0;
}
exp-1 = 1.000088900582e-12
expm1 = 1.000000000000e-12
How It Works
- Pick a tiny x. 1e-12 is near zero.
- Naive form.
exp(x) - 1 can be slightly off. - Stable form.
expm1(x) stays closer to x. - Lesson. Use the dedicated function for small deltas.
Example 3 — Percent-Style Growth
Turn a small rate into a growth factor minus one.
#include <stdio.h>
#include <math.h>
int main(void) {
double rate = 0.02;
double change = expm1(rate);
printf("relative change ≈ %.6f\n", change);
return 0;
}
relative change ≈ 0.020201
How It Works
- Choose a rate. 0.02 means about 2%.
- Call expm1. Convert continuous growth into a relative change.
- Read the result. Slightly above 0.02, as expected.
- Why expm1. Cleaner than
exp(rate) - 1 for small rates.
Frequently Asked Questions
expm1() returns exp(x) - 1. It is accurate even when x is very small.
x is required. It is the double exponent.
A double equal to exp(x) - 1. expm1(0) is 0.
Include <math.h>. On GCC, link with -lm.
Use expm1 when you need exp(x) - 1 and x may be near zero. Plain subtraction can lose precision.
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