C++ STL 中的 ilogb() 函式
在本文中,我們將討論 C++ 中 ilogb() 函式的工作原理、語法和示例。
什麼是 ilogb()?
ilogb() 函式是 C++ STL 中的一個內建函式,定義在 <cmath< 標頭檔案中。ilogb() 用於查詢對數值的整數部分。ilogb() 表示整數二進位制對數。
此函式使用 FLT_RADIX 作為對數的底數,返回 |x| 對數的整數部分。
語法
int ilogb(double x);
引數
該函式接受以下引數:
x- 這是我們必須查詢其對數的值。
返回值
此函式使用 FLT_RADIX 值作為底數,返回 |x| 的整數對數。根據引數值,此函式還引發異常。
如果引數值為:
NaN - 然後函式返回 FP_LOGBNAN。
無限大 - 然後函式返回 INT_MAX。
0 - 然後函式返回 FP_LOGB0
輸入
ilogb(2);
輸出
1
示例
#include <cfloat> #include <cmath> #include >iostream> using namespace std; int main(){ int output, var = 2; output = ilogb(var); cout << "The value of ilogb(" << var << ") is: " << output << endl; return 0; }
輸出
如果我們執行以上程式碼,它將生成以下輸出:
The value of ilogb(2) is: 1
示例
#include <cfloat> #include <cmath> #include <iostream> #include <iostream> using namespace std; int main(){ int output, var = 10.23; output = ilogb(var); cout << "The value of ilogb(" << var << ") is: " << output<< endl; return 0; }
輸出
如果我們執行以上程式碼,它將生成以下輸出:
The value of ilogb(10) is: 3
廣告