function
<cmath> <ctgmath>

log

double log (double x);
     double log  (     double x);      float logf (      float x);long double logl (long double x);
     double log (     double x);      float log (      float x);long double log (long double x);
     double log (double x);      float log (float x);long double log (long double x);     double log (T x);           // additional overloads for integral types
Compute natural logarithm
Returns the natural logarithm of x.

The natural logarithm is the base-e logarithm: the inverse of the natural exponential function (exp). For common (base-10) logarithms, see log10.

Header <tgmath.h> provides a type-generic macro version of this function.
This function is overloaded in <complex> and <valarray> (see complex log and valarray log).
Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double before calculations.

This function is also overloaded in <complex> and <valarray> (see complex log and valarray log).

Parameters

x
Value whose logarithm is calculated.
If the argument is negative, a domain error occurs.

Return Value

Natural logarithm of x.
If x is negative, it causes a domain error.
If x is zero, it may cause a pole error (depending on the library implementation).

If a domain error occurs, the global variable errno is set to EDOM.
If a pole error occurs, the global variable errno is set ERANGE.
If a domain error occurs:
- And math_errhandling has MATH_ERRNO set: the global variable errno is set to EDOM.
- And math_errhandling has MATH_ERREXCEPT set: FE_INVALID is raised.

If a pole error occurs:
- And math_errhandling has MATH_ERRNO set: the global variable errno is set to ERANGE.
- And math_errhandling has MATH_ERREXCEPT set: FE_DIVBYZERO is raised.

Example

1
2
3
4
5
6
7
8
9
10
11
12
/* log example */
#include <stdio.h>      /* printf */
#include <math.h>       /* log */

int main ()
{
  double param, result;
  param = 5.5;
  result = log (param);
  printf ("log(%f) = %f\n", param, result );
  return 0;
}

Output:

log(5.500000) = 1.704748


See also