Namespaces
Variants

ldexp, ldexpf, ldexpl

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
ldexp
(C99) (C99)
(C99) (C23)
(C99)
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
定义于头文件 <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 起)
1-3) 将浮点值 arg 乘以 2 exp 次幂。
4) 类型泛型宏:若 arg 具有 long double 类型,则调用 ldexpl 。否则,若 arg 具有整数类型或 double 类型,则调用 ldexp 。否则,分别调用 ldexpf

目录

参数

arg - 浮点数值
exp - 整数值

返回值

若无错误发生,则返回 arg 乘以 2 的 exp 次幂( arg×2 exp
)。

如果发生因上溢导致的范围错误,将返回 ±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 的效率低于使用算术运算符进行二的幂次乘除运算。

示例

#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));
    // 特殊值
    printf("ldexp(-0, 10) = %f\n", ldexp(-0.0, 10));
    printf("ldexp(-Inf, -1) = %f\n", ldexp(-INFINITY, -1));
    // 错误处理
    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 (最小正次正规双精度浮点数)
ldexp(nextafter(1,0), 1024) = 1.79769e+308 (最大有限双精度浮点数)
ldexp(-0, 10) = -0.000000
ldexp(-Inf, -1) = -inf
ldexp(1, 1024) = inf
    errno == ERANGE: 数值结果超出范围
    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函数(页码:225)
  • 7.22 泛型数学 <tgmath.h>(页码:335-337)
  • F.9.3.6 ldexp函数(页码:459)
  • C89/C90 标准 (ISO/IEC 9899:1990):
  • 4.5.4.3 ldexp 函数

参见

将数值分解为有效数字和 2 的幂次
(函数)
(C99) (C99) (C99) (C99) (C99) (C99)
高效计算数值乘以 FLT_RADIX 的指定幂次
(函数)
C++ 文档 关于 ldexp