log1p Function

Calculates the natural logarithm of a double precision floating-point value plus 1.

Include

<math.h>

Prototype

double log1p(double x);

Argument

x
any positive value for which to return the log

Return Value

Returns the natural logarithm of x+1. The result of log1p(x) is generally expected to be more accurate than that of log(x+1) when the magnitude of x is small.

Remarks

A domain error occurs if x is less than 1. A range error might occur if the argument equals 1.

Example

#include <math.h>
#include <stdio.h>
#include <errno.h>

int main(void)
{
  double x, y;

  errno = 0;
  x = 2.0;
  y = log1p(x);
  if(errno)
    perror("Error");
  printf("The result of log1p(%f) is %f\n", x, y);

  errno = 0;
  x = 0.0;
  y = log1p(x);
  if(errno)
    perror("Error");
  printf("The result of log1p(%f) is %f\n", x, y);

  errno = 0;
  x = -2.0;
  y = log1p(x);
  if(errno)
    perror("Error");
  printf("The result of log1p(%f) is %f\n", x, y);
}

Example Output

The result of log1p(2.000000) is 1.098612
The result of log1p(0.000000) is 0.000000
Error: domain error
The result of log1p(-2.000000) is nan