C++ log()
Published Sep 14, 2022Updated May 15, 2024
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:
Learn C++ on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours