Previous Page
Next Page

log

Calculates the natural logarithm of a number

#include <math.h>
double log ( double x  );
float logf ( float x  );         (C99)
long double logl ( long double x  );         (C99)

The log( ) functions calculate the natural logarithm of their argument. The natural logarithmcalled "log" for short in English as well as in Cis the logarithm to base e, where e is Euler's number, 2.718281....

The natural log of a number x is defined only for positive values of x. If x is negative, a domain error occurs; if x is zero, a range error may occur (or not, depending on the implementation).

Example

The following code prints some sample values for base 2, base e, and base 10 logarithms:

double x[ ] = { 1E-100, 0.5, 2, exp(1), 10, 1E+100 };

puts("       x           log2(x)            log(x)           log10(x)\n"
     " ---------------------------------------------------------------");
for ( int i = 0; i < sizeof(x) / sizeof(x[0]); ++i )
{
  printf("%#10.3G %+17.10G %+17.10G %+17.10G\n",
         x[i], log2(x[i]), log(x[i]), log10(x[i]) );
}

This code produces the following output:

       x           log2(x)            log(x)           log10(x)
 ---------------------------------------------------------------
 1.00E-100      -332.1928095      -230.2585093              -100
     0.500                -1     -0.6931471806     -0.3010299957
      2.00                +1     +0.6931471806     +0.3010299957
      2.72      +1.442695041                +1     +0.4342944819
      10.0      +3.321928095      +2.302585093                +1
 1.00E+100      +332.1928095      +230.2585093              +100

See Also

log10( ), log1p( ), log2( ), exp( ), pow( )


Previous Page
Next Page