What are some edge cases of log1pl?
The log1pl function
The log1pl function takes a long double argument (arg) and computes ln(1 + arg).
The log1pl function is defined in the math.h standard library with the following structure:
long double log1pl( long double arg );
Note: The
log1pl(arg)function is more precise thanlogl(1 + arg)ifargis closer to zero.
Parameters
arg: along doublenumber
Return value
- If no errors occur, then
ln(1 + arg)is returned. - If
argis positive or negative zero, thenargis returned. - If a domain error occurs (e.g., where
1 + argis less than0),NaNis returned. - If a pole error occurs (e.g., result of
ln(1 + arg)is infinity), is returned. - If
argis , is returned. - If
argisNaN, thenNaNis returned.
Example usage of the log1pf function
The following code provides a comparison between the log1pl and logl functions:
#include<stdio.h>#include<math.h>#include<float.h>int main() {long double x = 1e-20;long double y = 1 + x;printf("log1pl(1e-20) = %Lg\n", log1pl(x));printf("logl(1 + 1e-20) = %Lg\n", logl(y));}
In the example above, both functions compute ln(1 + e^(-20)), but log1pl gives a non-zero result. Therefore, log1pl must be used if the argument arg is close to zero since the result has higher precision.
The following code demonstrates the corner case inputs to the logpf function:
#include<stdio.h>#include<math.h>#include<float.h>int main() {printf("log1pl(+0.0) = %Lf\n", log1pl(+0.0));printf("log1pl(-0.0) = %Lf\n", log1pl(-0.0));printf("log1pl(+inf) = %Lf\n", log1pl(INFINITY));printf("log1pl(-1.0) = %Lf\n", log1pl(-1.0));}
Free Resources
Copyright ©2025 Educative, Inc. All rights reserved