C++ 中的ilogb() 函數返回|x| 的對數的整數部分,使用FLT_RADIX 作為對數的底。
這是在<cmath> 頭文件中定義的。
數學上,
x = significand * FLT_RADIXexponent
significand
是 [1.0, 2.0) 範圍內的浮點值,x 是傳遞給 ilogb() 的參數,exponent
是 ilogb() 返回的整數值。 FLT_RADIX
的值一般為2。
ilogb() 返回的值比 frexp() 函數生成的 index 小 1,因為有效數字在 [1.0, 2.0) 範圍內,而不是 frexp() 中的 [0.5, 1.0)。
ilogb() 原型 [從 C++ 11 標準開始]
int ilogb (double x); int ilogb (float x); int ilogb (long double x); int ilogb (T x); // For integral type
參數:
ilogb() 函數采用單個參數,計算其 ilogb。
返回:
ilogb() 函數返回 |x| 的對數的整數部分,使用 FLT_RADIX 作為對數的底。
- 如果參數為 0,則返回 FP_LOGB0。
- 如果參數為 NaN,則返回 FP_LOGBNAN。
- 如果參數是無限的,則返回INT_MAX。
示例 1:ilogb() 函數在 C++ 中如何工作?
#include <iostream>
#include <cmath>
#include <cfloat>
using namespace std;
int main()
{
int result;
double significand;
double x = 16.81;
result = ilogb(x);
significand = x / pow(FLT_RADIX, result);
cout << "ilogb (" << x << ") = " << result << endl;
cout << x << " = " << significand << " * " << FLT_RADIX << "^" << result << endl << endl;
return 0;
}
運行程序時,輸出將是:
ilogb (16.81) = 4 16.81 = 1.05062 * 2^4
示例 2:ilogb() 具有整數類型的函數
#include <iostream>
#include <cmath>
#include <cfloat>
using namespace std;
int main()
{
int result, x = 19;
result = ilogb(x);
double significand = x/pow(FLT_RADIX,result);
cout << "ilogb (" << x << ") = " << result << endl;
cout << x << " = " << significand << " * " << FLT_RADIX << "^" << result << endl << endl;
return 0;
}
運行程序時,輸出將是:
ilogb (19) = 4 19 = 1.1875 * 2^4
相關用法
- C++ ilogb()用法及代碼示例
- C++ is_unsigned用法及代碼示例
- C++ is_fundamental用法及代碼示例
- C++ iomanip setbase()用法及代碼示例
- C++ isdigit()用法及代碼示例
- C++ ios eof()用法及代碼示例
- C++ ios manipulators boolalpha()用法及代碼示例
- C++ ios Scientific用法及代碼示例
- C++ ios manipulators left()用法及代碼示例
- C++ iswdigit()用法及代碼示例
- C++ is_scalar用法及代碼示例
- C++ ios fixed用法及代碼示例
- C++ ios manipulators dec()用法及代碼示例
- C++ is_pointer用法及代碼示例
- C++ is_polymorphic用法及代碼示例
- C++ isalpha()用法及代碼示例
- C++ ios noshowbase用法及代碼示例
- C++ ios manipulators hex()用法及代碼示例
- C++ ios showpoint用法及代碼示例
- C++ iomanip setfill用法及代碼示例
注:本文由純淨天空篩選整理自 C++ ilogb()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。