- C 標準庫
- C 庫 - 首頁
- C 庫 - <assert.h>
- C 庫 - <complex.h>
- C 庫 - <ctype.h>
- C 庫 - <errno.h>
- C 庫 - <fenv.h>
- C 庫 - <float.h>
- C 庫 - <inttypes.h>
- C 庫 - <iso646.h>
- C 庫 - <limits.h>
- C 庫 - <locale.h>
- C 庫 - <math.h>
- C 庫 - <setjmp.h>
- C 庫 - <signal.h>
- C 庫 - <stdalign.h>
- C 庫 - <stdarg.h>
- C 庫 - <stdbool.h>
- C 庫 - <stddef.h>
- C 庫 - <stdio.h>
- C 庫 - <stdlib.h>
- C 庫 - <string.h>
- C 庫 - <tgmath.h>
- C 庫 - <time.h>
- C 庫 - <wctype.h>
- C 標準庫資源
- C 庫 - 快速指南
- C 庫 - 有用資源
- C 庫 - 討論
- C 程式設計資源
- C 程式設計 - 教程
- C - 有用資源
C 庫 - fesetexceptflag() 函式
C 的fenv 庫 fesetexceptflag() 函式用於透過 'excepts' 引數設定指定浮點異常標誌的狀態。此引數是浮點異常宏的按位或組合,例如FE_DIVBYZERO、FE_INEXACT、FE_INVALID、FE_OVERFLOW 和 FE_UNDERFLOW。
語法
以下是 fesetexceptflag() 函式的 C 庫語法 -
int fesetexceptflag(const fexcept_t *flagp, int excepts);
引數
此函式接受以下引數 -
-
flagp - 它表示指向 'fexcept_t' 物件的指標,標誌將儲存或從中讀取。
excepts - 它表示要設定的異常標誌的位掩碼列表。
返回值
如果此函式成功獲取指定異常的狀態,則返回 0,否則返回非零值。
示例 1
以下是用 fesetexceptflag() 設定單個浮點異常標誌的基本 C 示例。
#include <stdio.h>
#include <fenv.h>
int main() {
// Declare a variable to hold the exception flag
fexcept_t flag;
// Set the FE_OVERFLOW exception flag
if (fesetexceptflag(&flag, FE_OVERFLOW) != 0) {
printf("Failed to set the FE_OVERFLOW exception flag.\n");
return 1;
} else {
printf("Successfully set the FE_OVERFLOW exception flag.\n");
}
return 0;
}
輸出
以下是輸出 -
Successfully set the FE_OVERFLOW exception flag.
示例 2
以下 C 程式使用 fesetexceptflag() 設定 FE_INVALID 異常標誌。
#include <stdio.h>
#include <fenv.h>
int main() {
// Declare a variable to hold the exception flag
fexcept_t flag;
// Set the FE_OVERFLOW exception flag
if (fesetexceptflag(&flag, FE_INVALID) != 0) {
printf("Failed to set the FE_INVALID exception flag.\n");
return 1;
} else {
printf("Successfully set the FE_INVALID exception flag.\n");
}
return 0;
}
輸出
以下是輸出 -
Successfully set the FE_INVALID exception flag.
示例 3
這是另一個設定 FE_INVALID 異常標誌,然後檢查它是否已成功設定的示例。
#include <stdio.h>
#include <fenv.h>
int main() {
fexcept_t flag;
// Set the FE_INVALID exception flag
if (fesetexceptflag(&flag, FE_INVALID) != 0) {
printf("Failed to set the FE_INVALID exception flag.\n");
return 1;
} else {
printf("Successfully set the FE_INVALID exception flag.\n");
}
// Perform an invalid floating-point operation to trigger FE_INVALID
double result = 1.0 / 0.0;
// Check if FE_INVALID is raised
if (fetestexcept(FE_INVALID)) {
printf("FE_INVALID exception is raised due to division by zero.\n");
} else {
printf("FE_INVALID exception is not raised.\n");
}
return 0;
}
輸出
以下是輸出 -
Successfully set the FE_INVALID exception flag. FE_INVALID exception is not raised.
c_library_fenv_h.htm
廣告