CKTIS / C
log1p — C23 mathematical function
`log1p(x)` from `<math.h>` computes `ln(1 + x)` without the digit loss caused by separately forming `1 + x` for very small `x`; `log1pf` and `log1pl` serve the other types. Its real domain is `x >= -1`.
Version: C23
Syntax
double log1p(double x);log1p(x) from <math.h> computes ln(1 + x) without the digit loss caused by separately forming 1 + x for very small x; log1pf and log1pl serve the other types. Its real domain is x >= -1.
The example checks that log1p(1e-12) remains nonzero and prints log1p(1.0). Output 1 0.693 demonstrates preservation of the tiny increment and the value ln(2), so it exercises the function’s numerical advantage.
For x < -1 there is a domain error, while for x == -1 a pole error may occur. C23 requires a range error when nonzero x is too close to zero. Do not replace this function with log(1 + x) in numerical code. Annex F preserves the sign of zero, returns -∞ for -1 with FE_DIVBYZERO, propagates NaN, and returns +∞ for positive infinity.
Examples
#include <math.h>
#include <stdio.h>
int main(void){printf("%d %.3f\n",log1p(1e-12)!=0.0,log1p(1.0));return 0;}
Result
1 0.693
The example checks that `log1p(1e-12)` remains nonzero and prints `log1p(1.0)`. Output `1 0.693` demonstrates preservation of the tiny increment and the value `ln(2)`, so it exercises the function’s numerical advantage.