C庫 - fegetexceptflag() 函式



C 的fenvfegetexceptflag() 函式用於檢索由'excepts'引數指定的浮點異常標誌的當前狀態或完整內容。此引數是浮點異常宏的按位 OR 組合,例如FE_DIVBYZERO, FE_INEXACT, FE_INVALID, FE_OVERFLOW, 和 FE_UNDERFLOW

浮點異常標誌的完整內容不僅僅是指示是否發生異常的真/假值。相反,它可能更復雜,例如一個結構體,其中包含異常的狀態以及其他詳細資訊,例如導致異常的程式碼的地址。

語法

以下是fegetexceptflag() 函式的C庫語法:

int fegetexceptflag( fexcept_t* flagp, int excepts );

引數

此函式接受以下引數:

  • flagp − 它表示指向'fexcept_t'物件的指標,標誌將儲存在其中或從中讀取。

  • excepts − 它表示要獲取的異常標誌的位掩碼列表。

返回值

如果成功獲取指定異常的狀態,則此函式返回 0;否則返回非零值。

示例 1

以下是演示fegetexceptflag() 函式用法的基本C程式。

#include <stdio.h>
#include <fenv.h>

int main() {
   // Declare a variable to hold the exception flags
   fexcept_t flag;
   
   // Perform some floating-point operations that may raise exceptions
   double x = 1.0 / 0.0;
   
   // retrieve the state of the FE_DIVBYZERO exception flag
   if (fegetexceptflag(&flag, FE_DIVBYZERO) != 0) {
      printf("Failed to obtain the state of the FE_DIVBYZERO exception flag.\n");
      return 1;
   } else {
      printf("Successfully obtained the state of the FE_DIVBYZERO exception flag.\n");
   }
   return 0;
}

輸出

以下是輸出:

Successfully obtained the state of the FE_DIVBYZERO exception flag.

示例 2

以下C程式使用fegetexceptflag() 函式。在這裡,我們獲取多個異常標誌。

#include <stdio.h>
#include <fenv.h>
int main() {
   fexcept_t flag;
   
   // Perform some floating-point operations that may raise exceptions
   double x = 0.0 / 0.0;
   double y = 1e308 * 1e308;
   
   // Get the state of the FE_INVALID and FE_OVERFLOW exception flags
   if (fegetexceptflag(&flag, FE_INVALID | FE_OVERFLOW) != 0) {
      printf("Failed to obtain the state of the FE_INVALID and FE_OVERFLOW exception flags.\n");
      return 1;
   } else {
      printf("Successfully obtained the state of the FE_INVALID and FE_OVERFLOW exception flags.\n");
   }
   return 0;
}

輸出

以下是輸出:

Successfully obtained the state of the FE_INVALID and FE_OVERFLOW exception flags.

示例 3

下面的示例,如果成功獲取指定異常的狀態,則返回 0。

#include <stdio.h>
#include <fenv.h>
#include <math.h>
int main() {
   fexcept_t flag;

   // Perform some floating-point operations that may raise exceptions
   double x = 10.0 / 0.0;
   double square_root = sqrt(-1.0);

   // Get the state of the FE_INVALID and FE_OVERFLOW exception flags
   int res = fegetexceptflag(&flag, FE_INVALID | FE_OVERFLOW);
   printf("%d", res);
   return 0;
}

輸出

以下是輸出:

0
c_library_fenv_h.htm
廣告