Related Tags

c

# What are some edge cases of log1pf? Hammad Nasir

## The log1pf function

The log1pf function takes a float argument (arg) and computes ln(1 + arg).

The log1pf function is defined in the math.h standard library with the following structure:

float log1pf( float arg );


Note: The function log1pf(arg) is more precise than log(1 + arg) if arg is closer to zero.

## Parameters

• arg: a floating point 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 log1pf and log functions:

#include<stdio.h>
#include<math.h>
#include<float.h>
int main() {
printf("log1pf(1e-16) = %g\n", log1pf(1e-16));
printf("log(1 + 1e-16) = %g\n", log(1 + 1e-16));
}

In the example above, both functions compute ln(1 + e^(-16)), but log1pf gives a non-zero result. Therefore, log1pf 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("log1pf(+0.0) = %f\n", log1pf(+0.0));
printf("log1pf(-0.0) = %f\n", log1pf(-0.0));
printf("log1pf(+inf) = %f\n", log1pf(INFINITY));
printf("log1pf(-1.0) = %f\n", log1pf(-1.0));
}

RELATED TAGS

c

CONTRIBUTOR Hammad Nasir 