ldexp, ldexpf, ldexpl
來自 cppreference.com
定義於標頭檔案 <math.h> |
||
float ldexpf( float arg, int exp ); |
(1) | (C99 起) |
double ldexp( double arg, int exp ); |
(2) | |
long double ldexpl( long double arg, int exp ); |
(3) | (C99 起) |
定義於標頭檔案 <tgmath.h> |
||
#define ldexp( arg, exp ) |
(4) | (C99 起) |
4) 型別泛型宏:若 arg 為 long double 型別,則呼叫
ldexpl
。否則,若 arg 為整數型別或 double 型別,則呼叫 ldexp
。否則,呼叫 ldexpf
。目錄 |
[編輯] 引數
arg | - | 浮點值 |
exp | - | 整數值 |
[編輯] 返回值
若無錯誤發生,則返回 arg 乘以 2 的 exp 次冪(arg×2exp
)。
若發生因溢位導致的範圍錯誤,則返回 ±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)上,ldexp
等價於 scalbn。
函式 ldexp
(“載入指數”),連同其對偶函式 frexp,可用於操作浮點數的表示而無需直接的位操作。
在許多實現中,ldexp
比使用算術運算子乘以或除以 2 的冪效率更低。
[編輯] 示例
執行此程式碼
#include <errno.h> #include <fenv.h> #include <float.h> #include <math.h> #include <stdio.h> // #pragma STDC FENV_ACCESS ON int main(void) { printf("ldexp(7, -4) = %f\n", ldexp(7, -4)); printf("ldexp(1, -1074) = %g (minimum positive subnormal double)\n", ldexp(1, -1074)); printf("ldexp(nextafter(1,0), 1024) = %g (largest finite double)\n", ldexp(nextafter(1,0), 1024)); // special values printf("ldexp(-0, 10) = %f\n", ldexp(-0.0, 10)); printf("ldexp(-Inf, -1) = %f\n", ldexp(-INFINITY, -1)); // error handling errno = 0; feclearexcept(FE_ALL_EXCEPT); printf("ldexp(1, 1024) = %f\n", ldexp(1, 1024)); if (errno == ERANGE) perror(" errno == ERANGE"); if (fetestexcept(FE_OVERFLOW)) puts(" FE_OVERFLOW raised"); }
可能的輸出
ldexp(7, -4) = 0.437500 ldexp(1, -1074) = 4.94066e-324 (minimum positive subnormal double) ldexp(nextafter(1,0), 1024) = 1.79769e+308 (largest finite double) ldexp(-0, 10) = -0.000000 ldexp(-Inf, -1) = -inf ldexp(1, 1024) = inf errno == ERANGE: Numerical result out of range FE_OVERFLOW raised
[編輯] 引用
- C23 標準 (ISO/IEC 9899:2024)
- 7.12.6.6 ldexp 函式 (p: TBD)
- 7.25 型別通用數學 <tgmath.h> (p: TBD)
- F.10.3.6 ldexp 函式 (p: TBD)
- C17 標準 (ISO/IEC 9899:2018)
- 7.12.6.6 ldexp 函式 (p: TBD)
- 7.25 型別通用數學 <tgmath.h> (p: TBD)
- F.10.3.6 ldexp 函式 (p: TBD)
- C11 標準 (ISO/IEC 9899:2011)
- 7.12.6.6 ldexp 函式 (p: 244)
- 7.25 型別通用數學 <tgmath.h> (p: 373-375)
- F.10.3.6 ldexp 函式 (p: 522)
- C99 標準 (ISO/IEC 9899:1999)
- 7.12.6.6 ldexp 函式 (p: 225)
- 7.22 型別通用數學 <tgmath.h> (p: 335-337)
- F.9.3.6 ldexp 函式 (p: 459)
- C89/C90 標準 (ISO/IEC 9899:1990)
- 4.5.4.3 ldexp 函式
[編輯] 亦參見
(C99)(C99) |
將一個數字分解為有效數字和2的冪 (函式) |
(C99)(C99)(C99)(C99)(C99)(C99) |
高效計算一個數字乘以 FLT_RADIX 的冪 (函式) |
C++ 文件 關於 ldexp
|