C ++中的log()函数返回参数的自然对数(以e为底的对数)。
此函数在<cmath>头文件中定义。
logex = log(x)
double log (double x); float log (float x); long double log (long double x); double log (T x); //为整型
log()函数采用[0,∞]范围内的单个必需参数。
如果该值小于零,则log()返回NaN(非数字)。
log()函数返回数字的自然对数。
参数(x) | 返回VALUE |
---|---|
x> 1 | Positive |
x = 1 | 0 |
0> x> 1 | Negative |
x = 0 | -∞(-无穷大) |
x <0 | NaN (非数字) |
#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; }
运行该程序时,输出为:
log(x) = 2.56925 log(x) = nan
#include <iostream> #include <cmath> using namespace std; int main () { int x = 2; double result; result = log (x); cout << "log(x) = " << result << endl; return 0; }
运行该程序时,输出为:
log(x) = 0.693147