- 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 庫 - fegetenv() 函式
C 的fenv 庫 fegetenv() 函式設定帶小數的數字的控制流程。
假設我們正在處理一個數學問題,並且我們使用的是可以以不同方式進行數字舍入的計算器。但是,我們需要切換到具有不同舍入設定的不同計算器。因此,此函式就像儲存第一個計算器的狀態一樣,我們可以稍後返回並繼續使用與之前完全相同的設定。
語法
以下是 fegetenv() 函式的 C 庫語法。
int fegetenv(fenv_t *envp);
引數
此函式只接受一個引數:
- fen_v - 該函式接收指向 fenv_t 物件的指標,當前環境將儲存在此物件中。
返回值
此函式返回整數型別的返回值:
零,表示成功。
非零,表示無法獲取環境。
示例 1
以下 C 庫 fegetenv() 函式演示了儲存和恢復浮點環境的任務。
#include <stdio.h>
#include <fenv.h>
int main() {
fenv_t env;
// Save the current floating-point environment
fegetenv(&env);
// Perform some floating-point operations here
fesetenv(&env);
// Restore the saved environment
printf("Floating-point environment restored.\n");
return 0;
}
輸出
以上程式碼產生以下結果:
Floating-point environment restored.
示例 2
在此示例中,程式執行可能導致溢位的操作,恢復環境,並檢查是否發生了溢位。
#include <fenv.h>
#include <stdio.h>
// rounding direction mode
void rounding_mode()
{
printf("Rounding mode is ->");
switch (fegetround()) {
case FE_TONEAREST:
// Round to nearest
printf("FE_TONEAREST\n");
break;
case FE_DOWNWARD:
// Round downward
printf("FE_DOWNWARD\n");
break;
case FE_UPWARD:
// Round upward
printf("FE_UPWARD\n");
break;
case FE_TOWARDZERO:
// Round toward zero
printf("FE_TOWARDZERO\n");
break;
default:
printf("unknown\n");
};
}
int main(void)
{
fenv_t envp;
// initial environment
printf("Initial environment :\n");
// print the exception raised initially
printf("Exception raised -> \n");
if (fetestexcept(FE_ALL_EXCEPT)) {
if (fetestexcept(FE_DIVBYZERO))
printf("FE_DIVBYZERO \n");
if (fetestexcept(FE_INEXACT))
printf("FE_INEXACT \n");
if (fetestexcept(FE_INVALID))
printf("FE_INVALID \n");
if (fetestexcept(FE_OVERFLOW))
printf("FE_OVERFLOW \n");
if (fetestexcept(FE_UNDERFLOW))
printf("FE_UNDERFLOW \n");
}
else
printf("None\n");
// print the rounding direction mode
rounding_mode();
// Current environment
fegetenv(&envp);
feraiseexcept(FE_ALL_EXCEPT);
// Set rounding direction mode
fesetround(FE_DOWNWARD);
// after environment is change
printf("\nFinal environment :\n");
// print the exception raised
printf("Exception raised -> \n");
if (fetestexcept(FE_ALL_EXCEPT)) {
if (fetestexcept(FE_DIVBYZERO))
printf("FE_DIVBYZERO \n");
if (fetestexcept(FE_INEXACT))
printf("FE_INEXACT \n");
if (fetestexcept(FE_INVALID))
printf("FE_INVALID \n");
if (fetestexcept(FE_OVERFLOW))
printf("FE_OVERFLOW \n");
if (fetestexcept(FE_UNDERFLOW))
printf("FE_UNDERFLOW \n");
}
else
printf("None\n");
// print the rounding direction mode
rounding_mode();
return 0;
}
輸出
執行以上程式碼後,我們得到以下結果:
Initial environment : Exception raised -> None Rounding mode is ->FE_TONEAREST Final environment : Exception raised -> FE_DIVBYZERO FE_INEXACT FE_INVALID FE_OVERFLOW FE_UNDERFLOW Rounding mode is ->FE_DOWNWARD
c_library_fenv_h.htm
廣告