ldexp, ldexpf, ldexpl
来自 cppreference.cn
定义于头文件 <math.h> |
||
float ldexpf( float arg, int exp ); |
(1) | (since C99) |
double ldexp( double arg, int exp ); |
(2) | |
long double ldexpl( long double arg, int exp ); |
(3) | (since C99) |
定义于头文件 <tgmath.h> |
||
#define ldexp( arg, exp ) |
(4) | (since 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
(“load exponent”,加载指数),及其对偶函数 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 The ldexp functions (p: TBD)
- 7.25 Type-generic math <tgmath.h> (p: TBD)
- F.10.3.6 The ldexp functions (p: TBD)
- C17 标准 (ISO/IEC 9899:2018)
- 7.12.6.6 The ldexp functions (p: TBD)
- 7.25 Type-generic math <tgmath.h> (p: TBD)
- F.10.3.6 The ldexp functions (p: TBD)
- C11 标准 (ISO/IEC 9899:2011)
- 7.12.6.6 The ldexp functions (p: 244)
- 7.25 Type-generic math <tgmath.h> (p: 373-375)
- F.10.3.6 The ldexp functions (p: 522)
- C99 标准 (ISO/IEC 9899:1999)
- 7.12.6.6 The ldexp functions (p: 225)
- 7.22 Type-generic math <tgmath.h> (p: 335-337)
- F.9.3.6 The ldexp functions (p: 459)
- C89/C90 标准 (ISO/IEC 9899:1990)
- 4.5.4.3 The ldexp function
[编辑] 参见
(C99)(C99) |
将数字分解为尾数和 2 的幂 (function) |
(C99)(C99)(C99)(C99)(C99)(C99) |
高效计算数字乘以 FLT_RADIX 的幂 (function) |
C++ 文档 关于 ldexp
|