在头文件中定义float scalbnf( float arg, int exp );(1)(since C99)
double scalbn( double arg, int exp );(2)(since C99)
long double scalbnl( long double arg, int exp );(3)(since C99)
Defined in header
#define scalbn( arg, exp )(4)(since C99)
Defined in header
float scalblnf( float arg, long exp );(5)(since C99)
double scalbln( double arg, long exp );(6)(since C99)
long double scalblnl( long double arg, long exp );(7)(since C99)
Defined in header
#define scalbln( arg, exp )(8)(since C99)
1-3,5-7)将浮点值arg乘以FLT_RADIX功率exp。
4,8)型,通用宏:如果arg有型long double,scalbnl或scalblnl叫。否则,如果arg有整数类型或类型double,scalbn或者scalbln被调用。否则,scalbnf或scalblnf分别被调用。
参数
arg-浮点值exp-整数值
返回值
如果没有出现错误,arg乘以FLT_RADIX给力exp(ARG×FLT_RADIXexp
)返回。
如果范围误差由于发生溢出,±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),scalbn相当于ldexp。
虽然std::scalbn并被std::scalbln指定为有效地执行操作,但在许多实现中,它们比使用算术运算符的乘法或乘除法的效率要低。
scalbln提供该函数是因为从最小正浮点值缩放到最大有限值所需的因子可能大于标准保证的32767 INT_MAX。特别是对于80位long double,因子是32828。
GNU的实现不设置errno不分math_errhandling。
例
#include #include #include #include #include #pragma STDC FENV_ACCESS ON
int main(void){ printf("scalbn(7, -4) = %f\n", scalbn(7, -4)); printf("scalbn(1, -1074) = %g (minimum positive subnormal double)\n", scalbn(1, -1074)); printf("scalbn(nextafter(1,0), 1024) = %g (largest finite double)\n", scalbn(nextafter(1,0), 1024)); // special values printf("scalbn(-0, 10) = %f\n", scalbn(-0.0, 10)); printf("scalbn(-Inf, -1) = %f\n", scalbn(-INFINITY, -1)); //error handling
errno = 0; feclearexcept(FE_ALL_EXCEPT); printf("scalbn(1, 1024) = %f\n", scalbn(1, 1024)); if(errno == ERANGE) perror(" errno == ERANGE"); if(fetestexcept(FE_OVERFLOW)) puts(" FE_OVERFLOW raised");}
可能的输出:
scalbn(7, -4) = 0.437500scalbn(1, -1074) = 4.94066e-324 (minimum positive subnormal double)scalbn(nextafter(1,0), 1024) = 1.79769e+308 (largest finite double)scalbn(-0, 10) = -0.000000scalbn(-Inf, -1) = -infscalbn(1, 1024) = inf
errno == ERANGE: Numerical result out of range
FE_OVERFLOW raised
参考
C11标准(ISO / IEC 9899:2011):7.12.6.13 scalbn函数(p:247)
7.25类型通用数学(p:373-375)
F.10.3.13 scalbn函数(p:523)
C99标准(ISO / IEC 9899:1999):7.12.6.13 scalbn函数(p:228)
7.22类型通用数学(p:335-337)
F.9.3.13 scalbn函数(p:460)