CKTIS / C
expm1 — C23 mathematical function
`expm1(x)` from `<math.h>` computes `e^x - 1` as `double`; `expm1f` and `expm1l` serve `float` and `long double`. It is designed to retain accuracy for tiny `x`, where separately evaluating `exp(x) - 1` loses digits by subtracting nearby values.
Version: C23
Syntax
double expm1(double x);expm1(x) from <math.h> computes e^x - 1 as double; expm1f and expm1l serve float and long double. It is designed to retain accuracy for tiny x, where separately evaluating exp(x) - 1 loses digits by subtracting nearby values.
The example checks that expm1(1e-12) remains nonzero and evaluates expm1(log(2.0)). Output 1 1 demonstrates retention of a tiny increment and the identity e^(ln 2)-1 = 1.
Large positive x can overflow, while nonzero x sufficiently close to zero can underflow; inspect the mechanisms selected by math_errhandling. Do not replace this function with exp(x)-1 in numerically sensitive algorithms. Annex F guarantees expm1(±0)=±0, expm1(-∞)=-1, and expm1(+∞)=+∞.
Examples
#include <math.h>
#include <stdio.h>
int main(void) {
int tiny_survives = expm1(1e-12) != 0.0;
int identity_holds = fabs(expm1(log(2.0)) - 1.0) < 1e-12;
printf("%d %d\n", tiny_survives, identity_holds);
return 0;
}
Result
1 1
The example checks that `expm1(1e-12)` remains nonzero and evaluates `expm1(log(2.0))`. Output `1 1` demonstrates retention of a tiny increment and the identity `e^(ln 2)-1 = 1`.