log1pl
functionThe 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)
ifarg
is closer to zero.
arg
: a long double
numberln(1 + arg)
is returned.arg
is positive or negative zero, then arg
is returned.1 + arg
is less than 0
), NaN
is returned.ln(1 + arg)
is infinity), is returned.arg
is , is returned.arg
is NaN
, then NaN
is returned.log1pf
functionThe 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