?
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
在頭文件<fenv.h>中定義 | ||
---|---|---|
#define FE_DIVBYZERO /*implementation defined power of 2*/ | (自 C99) | |
#define FE_INEXACT /*implementation defined power of 2*/ | (自C99) | |
#define FE_INVALID /*implementation defined power of 2*/ | (自C99) | |
#define FE_OVERFLOW /*implementation defined power of 2*/ | (自 C99) | |
#define FE_UNDERFLOW /*implementation defined power of 2*/ | (自 C99) | |
#define FE_ALL_EXCEPT FE_DIVBYZERO | FE_INEXACT | \ FE_INVALID | FE_OVERFLOW | \ FE_UNDERFLOW | (自 C99) |
所有這些宏常量(FE_ALL_EXCEPT除外)都展開(kāi)為不同冪的2的整型常量表達(dá)式,它們唯一標(biāo)識(shí)所有受支持的浮點(diǎn)異常。 每個(gè)宏只在被支持時(shí)才被定義。
擴(kuò)展為所有其他FE_ *的按位OR的宏常量FE_ALL_EXCEPT始終定義,如果實(shí)現(xiàn)不支持浮點(diǎn)異常,則該常量將為零。
常量 | 說(shuō)明 |
---|---|
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é)果太大而無(wú)法表示 |
FE_UNDERFLOW | 早期浮點(diǎn)運(yùn)算的結(jié)果是低于正常的,并且精度下降 |
FE_ALL_EXCEPT | 按位或所有受支持的浮點(diǎn)異常 |
該實(shí)現(xiàn)可以在<fenv.h>中定義附加的宏常量來(lái)標(biāo)識(shí)附加的浮點(diǎn)異常。 所有這些常量都以FE_開(kāi)頭,后跟至少一個(gè)大寫(xiě)字母。
有關(guān)更多詳細(xì)信息,請(qǐng)參閱math_errhandling。
#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 / 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)
math_errhandlingMATH_ERRNOMATH_ERREXCEPT(C99)(C99)(C99) | 定義了常用數(shù)學(xué)函數(shù)(宏常量)使用的錯(cuò)誤處理機(jī)制, |
---|
| 用于浮點(diǎn)異常宏的C ++文檔 |