Name
log
Synopsis
Calculates the natural logarithm of a number
#include <math.h> doublelog
( doublex
); floatlogf
( floatx
); (C99) long doublelogl
( long doublex
); (C99)
The log()
functions
calculate the natural logarithm of their argument. The natural
logarithm—called “log” for short in English as well as in C—is 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
Get C in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.