📅  最后修改于: 2020-09-25 07:51:36             🧑  作者: Mango
该函数在
[Mathematics] log2x = log2(x) [In C++ Programming]
double log2(double x);
float log2(float x);
long double log2(long double x);
double log2(T x); // For integral type
log2() 函数采用单个参数,并返回double
, float
或long double
类型的值。
log2() 函数采用[0,∞]范围内的单个必需参数。
如果该值小于零,则log2()返回NaN
(非数字)。
log2() 函数返回数字的以2为底的对数。
Parameter (x) | Return Value |
---|---|
x > 1 | Positive |
x = 1 | Zero |
0 > x > 1 | Negative |
x = 0 | -∞ (- infinity) |
x < 0 | NaN (Not a Number) |
#include
#include
using namespace std;
int main ()
{
double x = 13.056, result;
result = log2(x);
cout << "log2(x) = " << result << endl;
x = -3.591;
result = log2(x);
cout << "log2(x) = " << result << endl;
return 0;
}
运行该程序时,输出为:
log2(x) = 3.70664
log2(x) = nan
#include
#include
using namespace std;
int main ()
{
double result;
int x = 2201;
result = log2(x);
cout << "log2(" << x << ") = " << result << endl;
return 0;
}
运行该程序时,输出为:
log2(2201) = 11.1039