# C++ Programming Code Examples

## Learn C++ Language

### Math Library log() Function in C++ Programming Language

**Math Library log() Function in C++**

**Syntax for Math log() Function in C++**

#include <cmath>
double log (double x);
float log (float x);
long double log (long double x);
double log (T x); // additional overloads for integral types

x

Value whose logarithm is calculated. If the argument is negative, a domain error occurs.
Function returns natural logarithm of x.
If x is negative, it causes a domain error.
If x is zero, it may cause a pole error (depending on the library implementation).
Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double before calculations.
This function is also overloaded in <complex> and <valarray> (see complex log and valarray log).
If a domain error occurs:
- And math_errhandling has MATH_ERRNO set: the global variable errno is set to EDOM.
- And math_errhandling has MATH_ERREXCEPT set: FE_INVALID is raised.
If a pole error occurs:
- And math_errhandling has MATH_ERRNO set: the global variable errno is set to ERANGE.
- And math_errhandling has MATH_ERREXCEPT set: FE_DIVBYZERO is raised.
/* log() function is an inbuilt function in C++ STL, which is defined in <complex> header file. log() returns complex natural logarithmic value of a complex value. The difference between the log() in math header file and log() of complex header file is that it is used to calculate the complex logarithmic where log() of math header file calculates normal logarithmic value. */
/* Compute natural logarithm by log() function code example */
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
double x = 13.056, result;
result = log (x);
cout << "log(x) = " << result << endl;
x = -3.591;
result = log (x);
cout << "log(x) = " << result << endl;
return 0;
}