C ++中的log()函數(shù)返回參數(shù)的自然對(duì)數(shù)(以e為底的對(duì)數(shù))。
此函數(shù)在<cmath>頭文件中定義。
logex = log(x)
double log (double x); float log (float x); long double log (long double x); double log (T x); //為整型
log()函數(shù)采用[0,∞]范圍內(nèi)的單個(gè)必需參數(shù)。
如果該值小于零,則log()返回NaN(非數(shù)字)。
log()函數(shù)返回?cái)?shù)字的自然對(duì)數(shù)。
參數(shù)(x) | 返回VALUE |
---|---|
x> 1 | Positive |
x = 1 | 0 |
0> x> 1 | Negative |
x = 0 | -∞(-無(wú)窮大) |
x <0 | NaN (非數(shù)字) |
#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; }
運(yùn)行該程序時(shí),輸出為:
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; }
運(yùn)行該程序時(shí),輸出為:
log(x) = 0.693147