- 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庫 - fetestexcept() 函式
C 的fenv庫 fetestexcept() 函式用於檢查透過'excepts'引數當前設定了哪些指定的浮點異常。
excepts 引數是異常宏的按位或組合,例如FE_DIVBYZERO, FE_INEXACT, FE_INVALID, FE_OVERFLOW, FE_UNDERFLOW, 和 FE_ALL_EXCEPT。
語法
以下是 fetestexcept() 函式的C庫語法:
int fetestexcept( int excepts );
引數
此函式接受單個引數:
-
excepts − 它表示要測試的異常標誌的位掩碼。
返回值
此函式返回一個整數值,它是對應於當前在 excepts 引數指定的那些異常標誌中設定的異常標誌的浮點異常宏的按位或。
示例 1
以下是演示 fetestexcept() 函式用法的基本 C 程式。
#include <stdio.h>
#include <fenv.h>
int main() {
// first clear all the exception
feclearexcept(FE_ALL_EXCEPT);
// divide by zero exception
double result = 1.0 / 0.0;
// Test which exception flags are set
int flags = fetestexcept(FE_DIVBYZERO);
if (flags & FE_DIVBYZERO) {
printf("Divide by zero exception is set.\n");
}
else{
printf("No exception is set. \n");
}
return 0;
}
輸出
以下是輸出:
Divide by zero exception is set.
示例 2
以下程式使用 fetestexcept() 函式檢查我們獲得了哪種型別的浮點異常。
#include <stdio.h>
#include <fenv.h>
#include <math.h>
int main() {
// first clear all the exception
feclearexcept(FE_ALL_EXCEPT);
double result = sqrt(-1.0);
// Test which exception flags are set
int flags = fetestexcept(FE_ALL_EXCEPT);
if (flags & FE_DIVBYZERO) {
printf("Divide by zero exception is set.\n");
}
if (flags & FE_INEXACT) {
printf("Inexact result exception is set.\n");
}
if (flags & FE_INVALID) {
printf("Invalid operation exception is set.\n");
}
if (flags & FE_OVERFLOW) {
printf("Overflow exception is set.\n");
}
if (flags & FE_UNDERFLOW) {
printf("Underflow exception is set.\n");
}
return 0;
}
輸出
以下是輸出:
Invalid operation exception is set.
示例 3
下面的示例在乘以大值後檢查溢位異常。
#include <stdio.h>
#include <fenv.h>
#include <math.h>
int main() {
// first Clear all exception flags
feclearexcept(FE_ALL_EXCEPT);
// overflow exception by multiplying large values
double overflow_result = 1.0e300 * 1.0e300;
// Test which exception flags
int flags = fetestexcept(FE_OVERFLOW);
if (flags & FE_OVERFLOW) {
printf("Overflow exception is set.\n");
}
else {
printf("No exception is set.\n");
}
return 0;
}
輸出
以下是輸出:
Overflow exception is set.
c_library_fenv_h.htm
廣告