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 (base- e ) logarithm of x+1 or NaN if a domain error occurs. 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

If x is less than -1, a domain error will occur and errno will be set to EDOM.

Example

See the notes at the beginning of this chapter or section for information on using printf() or scanf() (and other functions reading and writing the stdin or stdout streams) in the example code.

#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