?
Ce document utilise Manuel du site Web PHP chinois Libérer
在頭文件<fenv.h>中定義 | ||
---|---|---|
#define FE_DOWNWARD /*implementation defined*/ | (自 C99) | |
#define FE_TONEAREST /*implementation defined*/ | (自 C99) | |
#define FE_TOWARDZERO /*implementation defined*/ | (自C99) | |
#define FE_UPWARD /*implementation defined*/ | (自C99) |
這些宏常量中的每一個(gè)都擴(kuò)展為一個(gè)非負(fù)整數(shù)常量表達(dá)式,它可以與fesetround和fegetround一起使用來指示支持的浮點(diǎn)舍入模式之一。 該實(shí)現(xiàn)可以在<fenv.h>中定義附加的舍入模式常量,它們都應(yīng)以FE_開頭,后跟至少一個(gè)大寫字母。 每個(gè)宏只在被支持時(shí)才被定義。
在大多數(shù)實(shí)現(xiàn)中,這些宏常量擴(kuò)展為等于FLT_ROUNDS和float_round_style值的值。
常量 | 說明 |
---|---|
FE_DOWNWARD | 趨于負(fù)無窮 |
FE_TONEAREST | 向最接近的整數(shù)舍入 |
FE_TOWARDZERO | 四舍五入到零 |
FE_UPWARD | 向正無窮的方向前進(jìn) |
實(shí)現(xiàn)可能會(huì)支持其他舍入模式。
當(dāng)前舍入模式會(huì)影響以下內(nèi)容:
浮點(diǎn)算術(shù)運(yùn)算符在常量表達(dá)式之外的結(jié)果
double x = 1;x/10; // 0.09999999999999999167332731531132594682276248931884765625 // or 0.1000000000000000055511151231257827021181583404541015625
標(biāo)準(zhǔn)庫(kù)數(shù)學(xué)函數(shù)的結(jié)果
sqrt(2); // 1.41421356237309492343001693370752036571502685546875 // or 1.4142135623730951454746218587388284504413604736328125
浮點(diǎn)到浮點(diǎn)隱式轉(zhuǎn)換和轉(zhuǎn)換
double d = 1 + DBL_EPSILON;float f = d; // 1.00000000000000000000000 // or 1.00000011920928955078125
字符串轉(zhuǎn)換,如strtod
或printf
strtof("0.1", NULL); // 0.0999999940395355224609375 // or 0.100000001490116119384765625
庫(kù)四舍五入功能 nearbyint
, rint
, lrint
lrint(2.1); // 2 or 3
所有這些宏常量(FE_ALL_EXCEPT除外)都展開為不同冪的2的整型常量表達(dá)式,它們唯一標(biāo)識(shí)所有受支持的浮點(diǎn)異常。 每個(gè)宏只在被支持時(shí)才被定義。
擴(kuò)展為所有其他FE_ *的按位OR的宏常量FE_ALL_EXCEPT始終定義,如果實(shí)現(xiàn)不支持浮點(diǎn)異常,則該常量將為零。
常量 | 說明 |
---|---|
FE_DIVBYZERO | 極點(diǎn)錯(cuò)誤發(fā)生在較早的浮點(diǎn)運(yùn)算中 |
FE_INEXACT | 不精確的結(jié)果:舍入對(duì)于存儲(chǔ)較早浮點(diǎn)運(yùn)算的結(jié)果是必要的 |
FE_INVALID | 在早期的浮點(diǎn)操作中發(fā)生域錯(cuò)誤 |
FE_OVERFLOW | 較早浮點(diǎn)運(yùn)算的結(jié)果太大而無法表示 |
FE_UNDERFLOW | 早期浮點(diǎn)運(yùn)算的結(jié)果是低于正常的,并且精度下降 |
FE_ALL_EXCEPT | 按位或所有受支持的浮點(diǎn)異常 |
該實(shí)現(xiàn)可以在<fenv.h>中定義附加的宏常量來標(biāo)識(shí)附加的浮點(diǎn)異常。 所有這些常量都以FE_開頭,后跟至少一個(gè)大寫字母。
#include <stdio.h>#include <math.h>#include <float.h>#include <fenv.h> #pragma STDC FENV_ACCESS ONvoid show_fe_exceptions(void){ printf("exceptions raised:"); if(fetestexcept(FE_DIVBYZERO)) printf(" FE_DIVBYZERO"); if(fetestexcept(FE_INEXACT)) printf(" FE_INEXACT"); if(fetestexcept(FE_INVALID)) printf(" FE_INVALID"); if(fetestexcept(FE_OVERFLOW)) printf(" FE_OVERFLOW"); if(fetestexcept(FE_UNDERFLOW)) printf(" FE_UNDERFLOW"); feclearexcept(FE_ALL_EXCEPT); printf("\n");} int main(void){ printf("MATH_ERREXCEPT is %s\n", math_errhandling & MATH_ERREXCEPT ? "set" : "not set"); printf("0.0/0.0 = %f\n", 0.0/0.0); show_fe_exceptions(); printf("1.0/0.0 = %f\n", 1.0/0.0); show_fe_exceptions(); printf("1.0/10.0 = %f\n", 1.0/10.0); show_fe_exceptions(); printf("sqrt(-1) = %f\n", sqrt(-1)); show_fe_exceptions(); printf("DBL_MAX*2.0 = %f\n", DBL_MAX*2.0); show_fe_exceptions(); printf("nextafter(DBL_MIN/pow(2.0,52),0.0) = %.1f\n", nextafter(DBL_MIN/pow(2.0,52),0.0)); show_fe_exceptions();}
可能的輸出:
MATH_ERREXCEPT is set0.0/0.0 = nan exceptions raised: FE_INVALID1.0/0.0 = inf exceptions raised: FE_DIVBYZERO1.0/10.0 = 0.100000exceptions raised: FE_INEXACTsqrt(-1) = -nan exceptions raised: FE_INVALID DBL_MAX*2.0 = inf exceptions raised: FE_INEXACT FE_OVERFLOWnextafter(DBL_MIN/pow(2.0,52),0.0) = 0.0exceptions raised: FE_INEXACT FE_UNDERFLOW
C11標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.6 / 8浮點(diǎn)環(huán)境<fenv.h>(p:207)
C99標(biāo)準(zhǔn)(ISO / IEC 9899:1999):
7.6 / 7浮點(diǎn)環(huán)境<fenv.h>(p:188)
C11標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.6 / 6浮點(diǎn)環(huán)境<fenv.h>(p:207)
C99標(biāo)準(zhǔn)(ISO / IEC 9899:1999):
7.6 / 5浮點(diǎn)環(huán)境<fenv.h>(p:188)