std::log

From cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
Exponential functions
(C++11)
(C++11)
log
(C++11)
(C++11)
Power functions
(C++11)
(C++11)
Trigonometric and hyperbolic functions
(C++11)
(C++11)
(C++11)
Error and gamma functions
(C++11)
(C++11)
(C++11)
(C++11)
Nearest integer floating point operations
(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
Floating point manipulation functions
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
Classification/Comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Macro constants
(C++11)(C++11)(C++11)(C++11)(C++11)
 
Defined in header <cmath>
float       log( float arg );
double      log( double arg );
long double log( long double arg );
double      log( Integral arg );
(since C++11)

Computes the natural (base e) logarithm of arg.

Contents

[edit] Parameters

arg - value of floating-point or Integral type

[edit] Return value

Natural logarithm of arg.

Domain error occurs if arg is negative. NAN is returned in that case.

Range error occurs if arg is 0. -HUGE_VAL is returned in that case.

[edit] Example

The following code computes the binary (base 2) logarithm with help of natural logarithm.

#include <cmath>
#include <iostream>
 
int main()
{
    double base = 2.0;
    double arg  = 256.0;
    double result = std::log(arg) / std::log(base);
 
    std::cout << result << '\n';
}

Output:

8

[edit] See also

returns e raised to the given power (ex)
(function)
computes common (base 10) logarithm (log10(x))
(function)
raises a number to the given power (xy)
(function)
complex natural logarithm with the branch cuts along the negative real axis
(function template)
applies the function std::log to each element of valarray
(function template)
C documentation for log