C
数值 | Numerics

ldexp

ldexp, ldexpf, ldexpl

在头文件中定义
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)
Defined in header <tgmath.h>
#define ldexp( arg, exp )(4)(since C99)

1-3)arg将数字2 的浮点值乘以exp功率。

4)类型 - 通用宏:如果arg有类型long doubleldexpl被调用。否则,如果arg有整数类型或类型doubleldexp则调用。否则ldexpf,分别称为。

参数

arg-浮点值
exp-整数值

返回值

如果没有错误发生,arg乘以2的幂exparg×2exp

)返回。

如果范围误差由于发生溢出,±HUGE_VAL±HUGE_VALF,或±HUGE_VALL返回。

如果发生下溢引起的范围错误,则返回正确的结果(舍入后)。

错误处理

按照math_errhandling中的指定报告错误。

如果实现支持IEEE浮点运算(IEC 60559),

  • 除非发生范围错误,否则FE_INEXACT不会引发(结果是确切的)

笔记

二进制系统(其中,FLT_RADIX2),ldexp相当于scalbn

函数ldexp(“加载指数”)及其双数,frexp可以用来操纵浮点数的表示,而无需直接位操作。

在许多实现中,ldexp使用算术运算符的乘法或除法乘以二的幂的效率较低。

#include <stdio.h> #include <math.h> #include <float.h> #include <errno.h> #include <fenv.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

参考

  • C11标准(ISO / IEC 9899:2011):