C
数值 | Numerics

scalbnf

scalbn, scalbnf, scalbnl, scalbln, scalblnf, scalblnl

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

1-3,5-7)将浮点值arg乘以FLT_RADIX功率exp

4,8)型,通用宏:如果arg有型long doublescalbnlscalblnl叫。否则,如果arg有整数类型或类型doublescalbn或者scalbln被调用。否则,scalbnfscalblnf分别被调用。

参数

arg-浮点值
exp-整数值

返回值

如果没有出现错误,arg乘以FLT_RADIX给力exp(ARG×FLT_RADIXexp

)返回。

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

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

错误处理

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

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

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

笔记

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

虽然std::scalbn并被std::scalbln指定为有效地执行操作,但在许多实现中,它们比使用算术运算符的乘法或乘除法的效率要低。

scalbln提供该函数是因为从最小正浮点值缩放到最大有限值所需的因子可能大于标准保证的32767 INT_MAX。特别是对于80位long double,因子是32828。

GNU的实现不设置errno不分math_errhandling

#include <stdio.h> #include <math.h> #include <float.h> #include <errno.h> #include <fenv.h> #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.437500 scalbn(1, -1074) = 4.94066e-324 (minimum positive subnormal double) scalbn(nextafter(1,0), 1024) = 1.79769e+308 (largest finite double) scalbn(-0, 10) = -0.000000 scalbn(-Inf, -1) = -inf scalbn(1, 1024) = inf errno == ERANGE: Numerical result out of range FE_OVERFLOW raised

参考

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