log()

Published Sep 14, 2022Updated May 15, 2024
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 expressly 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:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn C++ on Codecademy