This function is defined in <cmath> header file.
[Mathematics] logex = log(x) [In C++ Programming]
log() prototype [As of C++ 11 standard]
double log (double x); float log (float x); long double log (long double x); double log (T x); // For integral type
log() Parameters
The log() function takes a single mandatory argument in the range [0, ∞].
If the value is less than zero, log() returns NaN
(Not a Number).
log() Return Value
The log() function returns the natural logarithm of a number.
Parameter (x) | Return VALUE |
---|---|
x > 1 | Positive |
x = 1 | 0 |
0 > x > 1 | Negative |
x = 0 | -∞ (- infinity) |
x < 0 | NaN (Not a Number) |
Example 1: How log() works?
#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;
}
When you run the program, the output will be:
log(x) = 2.56925 log(x) = nan
Example 2: log() With Integral Type
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
int x = 2;
double result;
result = log (x);
cout << "log(x) = " << result << endl;
return 0;
}
When you run the program, the output will be:
log(x) = 0.693147