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 than logl(1 + arg) if arg is closer to zero.

Parameters

  • arg: a long double number

Return value

  • If no errors occur, then ln(1 + arg) is returned.
  • If arg is positive or negative zero, then arg is returned.
  • If a domain error occurs (e.g., where 1 + arg is less than 0), NaN is returned.
  • If a pole error occurs (e.g., result of ln(1 + arg) is infinity), -\infty is returned.
  • If arg is ++\infty, ++\infty is returned.
  • If arg is NaN, then NaN is 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

HowDev By Educative. Copyright ©2025 Educative, Inc. All rights reserved