log()

The log() function returns the natural, base-e logarithm of the argument. This is the inverse of the exp() function.

Syntax

log(x)

If the x parameter is less than or equal to zero, then a domain error will occur. If x is equal to zero, a pole error may occur where the result has reached the point of infinity and cannot be expressedly returned.

Example

The following example uses the log() function to find the natural log of 5:

#include <iostream>
#include <math.h>
int main() {
double x = 5;
double result;
result = log(x);
std::cout << "The log of " << x << " is " << result << "\n";
// Output: The log of 5 is 1.60944
}

Codebyte Example

The following example is runnable and returns the natural log of 10:

Code
Output
Loading...

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn C++ on Codecademy