該函數計算給定數字的以 2 為底的對數。
假設一個數字是 'x':
log2(x) = log2x;
用法
float log2(float x);
double log2(double x);
long double log2(long double x);
double log2(integral x);
注意:return_type 可以是 float、double 或 long double。
參數
x:要計算其對數的值。
返回值
參數 | 返回值 |
---|---|
x>1 | Positive |
x=1 | Zero |
1>x> 0 | Negative |
x= 0 | -無窮大 |
x<0 | 不是數字(nan) |
例子1
讓我們看看當 x 的值大於 1 時的簡單示例。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int x=2;
std::cout << "Value of x is:" <<x <<std::endl;
cout<<"log2(x) = "<<log2(x);
return 0;}
輸出:
Value of x is:2 log2(x) = 1
在此示例中,log2() 函數計算 x 的值大於 1 時以 2 為底的對數值
例子2
讓我們看一個簡單的例子,當 x 的值等於 1 時。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int x=1;
std::cout << "Value of x is:" <<x <<std::endl;
cout<<"log2(x) = "<<log2(x);
return 0;
}.
輸出:
Value of x is:1 log2(x) = 0
在本例中,log2() 函數計算 x 等於 1 時以 2 為底的對數值。
例子3
讓我們看一個簡單的例子,當 x 的值介於 0 和 1 之間時。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x=0.2;
std::cout << "Value of x is:" <<x <<std::endl;
cout<<"log2(x) = "<<log2(x);
return 0;
}
輸出:
Value of x is:0.2 log2(x) = -2.32193
在此示例中,log2() 函數計算 x 的值等於 0.2 時以 2 為底的對數值。
示例 4
讓我們看一個簡單的例子,當 x 的值等於 0 時。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int x=0;
std::cout << "Value of x is:" <<x <<std::endl;
cout<<"log2(x) = "<<log2(x);
return 0;
}
輸出:
Value of x is:0 log2(x) = -inf
在本例中,log2() 函數計算 x 等於 0 時以 2 為底的對數值。
例 5
讓我們看看 x 的值小於零時的簡單示例。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x= -1.50;
std::cout << "Value of x is:" <<x <<std::endl;
cout<<"log2(x) = "<<log2(x);
return 0;
}
輸出:
Value of x is:-1.5 log2(x) = nan
在此示例中,當 x 的值小於零時,log2() 函數計算以 2 為底的對數值。
相關用法
- C++ Math log()用法及代碼示例
- C++ Math log10()用法及代碼示例
- C++ Math logb()用法及代碼示例
- C++ Math log1p()用法及代碼示例
- C++ Math llround()用法及代碼示例
- C++ Math less()用法及代碼示例
- C++ Math lgamma()用法及代碼示例
- C++ Math lround()用法及代碼示例
- C++ Math lrint()用法及代碼示例
- C++ Math llrint()用法及代碼示例
- C++ Math scalbn()用法及代碼示例
- C++ Math acosh()用法及代碼示例
- C++ Math asinh()用法及代碼示例
- C++ Math isgreater()用法及代碼示例
- C++ Math fabs()用法及代碼示例
- C++ Math islessgreater()用法及代碼示例
- C++ Math nearbyint()用法及代碼示例
- C++ Math tan()用法及代碼示例
- C++ Math nextafter()用法及代碼示例
- C++ Math fdim()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Math log2()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。