ldexpf
在头文件<stdlib.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) |
Defined in header <tgmath.h> | | |
#define ldexp( arg, exp ) | (4) | (since C99) |
1-3)arg
将数字2 的浮点值乘以exp
功率。
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
使用算术运算符的乘法或除法乘以二的幂的效率较低。
例
#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):
- 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函数
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com