名稱空間
變體
操作

fetestexcept

來自 cppreference.com
< c‎ | 數值‎ | fenv
定義於標頭檔案 <fenv.h>
int fetestexcept( int excepts );
(C99 起)

確定指定的浮點異常子集中哪些當前已設定。引數 excepts浮點異常宏的按位或。

目錄

[編輯] 引數

excepts - 列出要測試的異常標誌的位掩碼

[編輯] 返回值

excepts 中包含的且與當前設定的浮點異常對應的浮點異常宏的按位或。

[編輯] 示例

#include <stdio.h>
#include <math.h>
#include <fenv.h>
#include <float.h>
 
#pragma STDC FENV_ACCESS ON
 
void show_fe_exceptions(void)
{
    printf("current 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");
    if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
    printf("\n");
}
 
int main(void)
{
    /* Show default set of exception flags. */
    show_fe_exceptions();
 
    /* Perform some computations which raise exceptions. */
    printf("1.0/0.0     = %f\n", 1.0/0.0);        /* FE_DIVBYZERO            */
    printf("1.0/10.0    = %f\n", 1.0/10.0);       /* FE_INEXACT              */
    printf("sqrt(-1)    = %f\n", sqrt(-1));       /* FE_INVALID              */
    printf("DBL_MAX*2.0 = %f\n", DBL_MAX*2.0);    /* FE_INEXACT FE_OVERFLOW  */
    printf("nextafter(DBL_MIN/pow(2.0,52),0.0) = %.1f\n",
           nextafter(DBL_MIN/pow(2.0,52),0.0));   /* FE_INEXACT FE_UNDERFLOW */
    show_fe_exceptions();
 
    return 0;
}

輸出

current exceptions raised:  none
1.0/0.0     = inf
1.0/10.0    = 0.100000
sqrt(-1)    = -nan
DBL_MAX*2.0 = inf
nextafter(DBL_MIN/pow(2.0,52),0.0) = 0.0
current exceptions raised:  FE_DIVBYZERO FE_INEXACT FE_INVALID FE_OVERFLOW FE_UNDERFLOW

[編輯] 參考

  • C11 標準 (ISO/IEC 9899:2011)
  • 7.6.2.5 fetestexcept 函式 (p: 211-212)
  • C99 標準 (ISO/IEC 9899:1999)
  • 7.6.2.5 fetestexcept 函式 (p: 192-193)

[編輯] 參閱

清除指定的浮點狀態標誌
(函式) [編輯]
C++ 文件,關於 fetestexcept