logb, logbf, logbl
來自 cppreference.com
定義於標頭檔案 <math.h> |
||
float logbf( float arg ); |
(1) | (C99 起) |
double logb( double arg ); |
(2) | (C99 起) |
long double logbl( long double arg ); |
(3) | (C99 起) |
定義於標頭檔案 <tgmath.h> |
||
#define logb( arg ) |
(4) | (C99 起) |
1-3) 從浮點引數 arg 中提取無偏、與基數無關的指數值,並將其作為浮點值返回。
4) 泛型宏:如果 arg 的型別為 long double,則呼叫
logbl
。否則,如果 arg 的型別為整數型別或 double,則呼叫 logb
。否則,呼叫 logbf
。形式上,對於非零 arg,無偏指數是 logr|arg| 的有符號整數部分(此函式將其作為浮點值返回),其中 r
是 FLT_RADIX。如果 arg 是次正規數,則將其視為已歸一化。
目錄 |
[編輯] 引數
arg | - | 浮點值 |
[編輯] 返回值
如果沒有發生錯誤,則返回 arg 的無偏指數作為帶符號浮點值。
若發生定義域錯誤,返回實現定義的值。
如果發生極點錯誤,則返回 -HUGE_VAL、-HUGE_VALF
或 -HUGE_VALL
。
[編輯] 錯誤處理
錯誤按 math_errhandling
中指定的方式報告。
如果 arg 為零,則可能發生定義域錯誤或範圍錯誤。
如果實現支援 IEEE 浮點運算 (IEC 60559),
- 如果 arg 為 ±0,則返回 -∞ 並引發 FE_DIVBYZERO。
- 如果 arg 為 ±∞,則返回 +∞。
- 如果 arg 是 NaN,則返回 NaN。
- 在所有其他情況下,結果是精確的(從不引發 FE_INEXACT),並忽略當前舍入模式。
[編輯] 注意
POSIX 要求,如果 arg 為 ±0,則發生極點錯誤。
logb
返回的指數值總是比 frexp 返回的指數值小 1,因為歸一化要求不同:對於 logb
返回的指數 e
,|arg*r-e
| 介於 1 和 r
之間(通常介於 1 和 2 之間),但對於 frexp 返回的指數 e
,|arg*2-e
| 介於 0.5 和 1 之間。
[編輯] 示例
比較不同的浮點分解函式。
執行此程式碼
#include <fenv.h> #include <float.h> #include <math.h> #include <stdio.h> // #pragma STDC FENV_ACCESS ON int main(void) { double f = 123.45; printf("Given the number %.2f or %a in hex,\n", f, f); double f3; double f2 = modf(f, &f3); printf("modf() makes %.0f + %.2f\n", f3, f2); int i; f2 = frexp(f, &i); printf("frexp() makes %f * 2^%d\n", f2, i); i = logb(f); printf("logb()/logb() make %f * %d^%d\n", f/scalbn(1.0, i), FLT_RADIX, i); // error handling feclearexcept(FE_ALL_EXCEPT); printf("logb(0) = %f\n", logb(0)); if (fetestexcept(FE_DIVBYZERO)) puts(" FE_DIVBYZERO raised"); }
可能的輸出
Given the number 123.45 or 0x1.edccccccccccdp+6 in hex, modf() makes 123 + 0.45 frexp() makes 0.964453 * 2^7 logb()/logb() make 1.928906 * 2^6 logb(0) = -Inf FE_DIVBYZERO raised
[編輯] 參考
- C23 標準 (ISO/IEC 9899:2024)
- 7.12.6.11 logb 函式 (p: TBD)
- 7.25 型別通用數學 <tgmath.h> (p: TBD)
- F.10.3.11 logb 函式 (p: TBD)
- C17 標準 (ISO/IEC 9899:2018)
- 7.12.6.11 logb 函式 (p: 179-180)
- 7.25 型別通用數學 <tgmath.h> (p: 373-375)
- F.10.3.11 logb 函式 (p: 381)
- C11 標準 (ISO/IEC 9899:2011)
- 7.12.6.11 logb 函式 (p: 246)
- 7.25 型別通用數學 <tgmath.h> (p: 373-375)
- F.10.3.11 logb 函式 (p: 522)
- C99 標準 (ISO/IEC 9899:1999)
- 7.12.6.11 logb 函式 (p: 227)
- 7.22 型別通用數學 <tgmath.h> (p: 335-337)
- F.9.3.11 logb 函式 (p: 459)
[編輯] 參閱
(C99)(C99) |
將一個數字分解為有效數字和2的冪 (函式) |
(C99)(C99)(C99) |
提取給定數字的指數 (函式) |
(C99)(C99)(C99)(C99)(C99)(C99) |
高效計算一個數字乘以 FLT_RADIX 的冪 (函式) |
C++ 文件 for logb
|