scalbn, scalbnf, scalbnl, scalbln, scalblnf, scalblnl
來自 cppreference.com
定義於標頭檔案 <math.h> |
||
float scalbnf( float arg, int exp ); |
(1) | (C99 起) |
double scalbn( double arg, int exp ); |
(2) | (C99 起) |
long double scalbnl( long double arg, int exp ); |
(3) | (C99 起) |
定義於標頭檔案 <tgmath.h> |
||
#define scalbn( arg, exp ) |
(4) | (C99 起) |
定義於標頭檔案 <math.h> |
||
float scalblnf( float arg, long exp ); |
(5) | (C99 起) |
double scalbln( double arg, long exp ); |
(6) | (C99 起) |
long double scalblnl( long double arg, long exp ); |
(7) | (C99 起) |
定義於標頭檔案 <tgmath.h> |
||
#define scalbln( arg, exp ) |
(8) | (C99 起) |
4,8) 型別通用宏:如果 arg 的型別是 long double,則呼叫
scalbnl
或 scalblnl
。否則,如果 arg 具有整型或 double 型別,則呼叫 scalbn
或 scalbln
。否則,分別呼叫 scalbnf
或 scalblnf
。目錄 |
[編輯] 引數
arg | - | 浮點值 |
exp | - | 整數值 |
[編輯] 返回值
如果沒有發生錯誤,返回 arg 乘以 FLT_RADIX 的 exp 次冪(arg×FLT_RADIXexp
)。
如果由於溢位導致範圍錯誤,返回 ±HUGE_VAL、±HUGE_VALF
或 ±HUGE_VALL
。
如果發生因下溢導致的範圍錯誤,返回正確的結果(舍入後)。
[編輯] 錯誤處理
錯誤按 math_errhandling
中指定的方式報告。
如果實現支援 IEEE 浮點運算 (IEC 60559),
- 除非發生範圍錯誤,否則絕不會引發 FE_INEXACT(結果是精確的)。
- 除非發生範圍錯誤,否則忽略當前舍入模式。
- 如果 arg 是 ±0,則返回它,不作修改。
- 如果 arg 是 ±∞,則返回它,不作修改。
- 如果 exp 為 0,則返回
arg
,不作修改。 - 如果 arg 是 NaN,則返回 NaN。
[編輯] 注意
在二進位制系統上(其中 FLT_RADIX 為 2
),scalbn
等效於 ldexp。
儘管 scalbn
和 scalbln
被指定為高效執行操作,但在許多實現中,它們不如使用算術運算子的乘法或除以二的冪高效。
提供 scalbln
函式是因為從最小正浮點值縮放到最大有限值所需的因子可能大於 32767,即標準保證的 INT_MAX。特別是,對於 80 位 long double,該因子為 32828。
[編輯] 示例
執行此程式碼
#include <errno.h> #include <fenv.h> #include <float.h> #include <math.h> #include <stdio.h> // #pragma STDC FENV_ACCESS ON int main(void) { printf("scalbn(7, -4) = %f\n", scalbn(7, -4)); printf("scalbn(1, -1074) = %g (minimum positive subnormal double)\n", scalbn(1, -1074)); printf("scalbn(nextafter(1,0), 1024) = %g (largest finite double)\n", scalbn(nextafter(1,0), 1024)); // special values printf("scalbn(-0, 10) = %f\n", scalbn(-0.0, 10)); printf("scalbn(-Inf, -1) = %f\n", scalbn(-INFINITY, -1)); // error handling errno = 0; feclearexcept(FE_ALL_EXCEPT); printf("scalbn(1, 1024) = %f\n", scalbn(1, 1024)); if (errno == ERANGE) perror(" errno == ERANGE"); if (fetestexcept(FE_OVERFLOW)) puts(" FE_OVERFLOW raised"); }
可能的輸出
scalbn(7, -4) = 0.437500 scalbn(1, -1074) = 4.94066e-324 (minimum positive subnormal double) scalbn(nextafter(1,0), 1024) = 1.79769e+308 (largest finite double) scalbn(-0, 10) = -0.000000 scalbn(-Inf, -1) = -inf scalbn(1, 1024) = inf errno == ERANGE: Numerical result out of range FE_OVERFLOW raised
[編輯] 參考
- C23 標準 (ISO/IEC 9899:2024)
- 7.12.6.13 The scalbn functions (p: TBD)
- 7.25 型別通用數學 <tgmath.h> (p: TBD)
- F.10.3.13 The scalbn functions (p: TBD)
- C17 標準 (ISO/IEC 9899:2018)
- 7.12.6.13 The scalbn functions (p: TBD)
- 7.25 型別通用數學 <tgmath.h> (p: TBD)
- F.10.3.13 The scalbn functions (p: TBD)
- C11 標準 (ISO/IEC 9899:2011)
- 7.12.6.13 The scalbn functions (p: 247)
- 7.25 型別通用數學 <tgmath.h> (p: 373-375)
- F.10.3.13 The scalbn functions (p: 523)
- C99 標準 (ISO/IEC 9899:1999)
- 7.12.6.13 The scalbn functions (p: 228)
- 7.22 型別通用數學 <tgmath.h> (p: 335-337)
- F.9.3.13 The scalbn functions (p: 460)
[編輯] 另請參閱
(C99)(C99) |
將一個數字分解為有效數字和2的冪 (函式) |
(C99)(C99) |
將一個數字乘以2的冪 (函式) |
C++ documentation for scalbn
|