log()

Published Sep 14, 2022Updated Dec 21, 2022
Contribute to Docs

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...

All contributors

Looking to contribute?

Learn C++ on Codecademy