?
本文檔使用 PHP中文網(wǎng)手冊(cè) 發(fā)布
在頭文件<math.h>中定義 | ||
---|---|---|
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)將浮點(diǎn)值arg
乘以FLT_RADIX
功率exp
。
如果實(shí)現(xiàn)支持IEEE浮點(diǎn)運(yùn)算(IEC 60559),對(duì)于round
,roundf
和roundl
功能:
當(dāng)前的舍入模式不起作用。
如果arg
是±∞,則返回,未修改
如果arg
為±0,則返回,未修改
如果arg
是NaN,則返回NaN
對(duì)于lround
和llround
功能家庭:
FE_INEXACT
從未被提出
當(dāng)前的舍入模式不起作用。
如果arg
是±∞,FE_INVALID
則引發(fā)并返回實(shí)現(xiàn)定義的值
如果舍入的結(jié)果超出返回類型的范圍,FE_INVALID
則會(huì)引發(fā)并返回實(shí)現(xiàn)定義的值
如果arg
是NaN,FE_INVALID
則引發(fā)并返回實(shí)現(xiàn)定義的值
FE_INEXACT
可能是(但不要求)round
在舍入非整數(shù)有限值時(shí)引發(fā)。
最大的可表示浮點(diǎn)值是所有標(biāo)準(zhǔn)浮點(diǎn)格式中的精確整數(shù),因此round
不會(huì)自行溢出; 但是intmax_t
,當(dāng)存儲(chǔ)在整數(shù)變量中時(shí),結(jié)果可能會(huì)溢出任何整數(shù)類型(包括)。
POSIX規(guī)定是,所有病例lround
或llround
提高FE_INEXACT
是域錯(cuò)誤。
該行為的double
版本round
如同執(zhí)行如下:
按照math_errhandling中的指定報(bào)告錯(cuò)誤。
#include <math.h>#include <fenv.h>#pragma STDC FENV_ACCESS ON double round(double x){ fenv_t save_env; feholdexcept(&save_env); double result = rint(x); if (fetestexcept(FE_INEXACT)) { fesetround(FE_TOWARDZERO); result = rint(copysign(0.5 + fabs(x), x)); } feupdateenv(&save_env); return result;}
除非發(fā)生范圍錯(cuò)誤,否則FE_INEXACT
不會(huì)引發(fā)(結(jié)果是確切的)
除非發(fā)生范圍錯(cuò)誤,否則當(dāng)前舍入模式將被忽略
如果arg
為±0,則返回,未修改
如果arg
是±∞,則返回,未修改
如果exp
為0,則arg
返回,未修改
如果arg
是NaN,則返回NaN
二進(jìn)制系統(tǒng)(其中,FLT_RADIX
是2
),scalbn
相當(dāng)于ldexp
。
#include <stdio.h>#include <math.h>#include <fenv.h>#include <limits.h> #pragma STDC FENV_ACCESS ON int main(void){ // round printf("round(+2.3) = %+.1f ", round(2.3)); printf("round(+2.5) = %+.1f ", round(2.5)); printf("round(+2.7) = %+.1f\n", round(2.7)); printf("round(-2.3) = %+.1f ", round(-2.3)); printf("round(-2.5) = %+.1f ", round(-2.5)); printf("round(-2.7) = %+.1f\n", round(-2.7)); printf("round(-0.0) = %+.1f\n", round(-0.0)); printf("round(-Inf) = %+f\n", round(-INFINITY)); // lround printf("lround(+2.3) = %ld ", lround(2.3)); printf("lround(+2.5) = %ld ", lround(2.5)); printf("lround(+2.7) = %ld\n", lround(2.7)); printf("lround(-2.3) = %ld ", lround(-2.3)); printf("lround(-2.5) = %ld ", lround(-2.5)); printf("lround(-2.7) = %ld\n", lround(-2.7)); printf("lround(-0.0) = %ld\n", lround(-0.0)); printf("lround(-Inf) = %ld\n", lround(-INFINITY)); // FE_INVALID raised // error handling feclearexcept(FE_ALL_EXCEPT); printf("lround(LONG_MAX+1.5) = %ld\n", lround(LONG_MAX+1.5)); if(fetestexcept(FE_INVALID)) puts(" FE_INVALID was raised");}
scalbln
提供該函數(shù)是因?yàn)閺淖钚≌↑c(diǎn)值縮放到最大有限值所需的因子可能大于標(biāo)準(zhǔn)保證的32767 INT_MAX
。特別是對(duì)于80位long double
,因子是32828。
可能的輸出:
#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");}
C11標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.12.9.6輪次函數(shù)(p:253)
7.12.9.7地下和地下功能(p:253)
7.25類型通用數(shù)學(xué)<tgmath.h>(p:373-375)
F.10.6.6循環(huán)函數(shù)(p:527)
F.10.6.7基礎(chǔ)和基本功能(p:528)
C99標(biāo)準(zhǔn)(ISO / IEC 9899:1999):
7.12.9.6輪次函數(shù)(p:233)
7.12.9.7理論和實(shí)踐(p:234)
7.22類型通用數(shù)學(xué)<tgmath.h>(p:335-337)
F.9.6.6循環(huán)函數(shù)(p:464)
F.9.6.7地下和地下功能(p:464)
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標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.12.6.13 scalbn函數(shù)(p:247)
7.25類型通用數(shù)學(xué)<tgmath.h>(p:373-375)
F.10.3.13 scalbn函數(shù)(p:523)
C99標(biāo)準(zhǔn)(ISO / IEC 9899:1999):
7.12.6.13 scalbn函數(shù)(p:228)
7.22類型通用數(shù)學(xué)<tgmath.h>(p:335-337)
F.9.3.13 scalbn函數(shù)(p:460)