Previous Page
Next Page

log1p

Calculates the logarithm of one plus a number

#include <math.h>
double log1p ( double x  );
float log1pf ( float x  );         (C99)
long double log1pl ( long double x  );         (C99)

The log1p( ) functions calculate the natural logarithm of the sum of one plus the argument x, or loge(1 + x). The function is designed to yield a more accurate result than the expression log(x + 1), especially when the value of the argument is close to zero.

The natural logarithm is defined only for positive numbers. If x is less than -1, a domain error occurs; if x is equal to -1, a range error may occur (or not, depending on the implementation).

Example

// atanh(x) is defined as 0.5 * ( log(x+1) - log(-x+1).
// Rounding errors can result in different results for different methods.

puts("   x          atanh(x)     atanh(x) - 0.5*(log1p(x) - log1p(-x))\n"
     "---------------------------------------------------------------");
for ( double x = -0.8; x < 1.0; x += 0.4)
{
  double y = atanh(x);
  printf("%6.2f %15f %20E\n", x, y, y - 0.5*(log1p(x) - log1p(-x)) );
}

This code produces the following output:

   x          atanh(x)     atanh(x) - 0.5*(log1p(x) - log1p(-x))
---------------------------------------------------------------
 -0.80       -1.098612        -1.376937E-17
 -0.40       -0.423649        -1.843144E-18
  0.00        0.000000         0.000000E+00
  0.40        0.423649         7.589415E-19
  0.80        1.098612        -4.640385E-17

See Also

log( ), log10( ), log2( ), exp( ), pow( )


Previous Page
Next Page