#include <stdio.h>
#include <fenv.h>
#include <math.h>
#include <stdbool.h>
int main(void) {
feclearexcept(FE_ALL_EXCEPT);
volatile double one = 1.0;
volatile double zero = 0.0;
volatile double div_res = one / zero;
(void)div_res;
int div_flag = (fetestexcept(FE_DIVBYZERO) != 0) ? 1 : 0;
feclearexcept(FE_ALL_EXCEPT);
volatile double neg = -1.0;
volatile double sq_res = sqrt(neg);
(void)sq_res;
int inv_flag = (fetestexcept(FE_INVALID) != 0) ? 1 : 0;
feclearexcept(FE_ALL_EXCEPT);
feraiseexcept(FE_OVERFLOW);
fexcept_t saved_flag;
fegetexceptflag(&saved_flag, FE_OVERFLOW);
feclearexcept(FE_ALL_EXCEPT);
fesetexceptflag(&saved_flag, FE_OVERFLOW);
int ov_flag = (fetestexcept(FE_OVERFLOW) != 0) ? 1 : 0;
feclearexcept(FE_ALL_EXCEPT);
bool c1_pass = (div_flag == 1 && inv_flag == 1 && ov_flag == 1);
int orig_round = fegetround();
double (*volatile rint_fn)(double) = rint;
volatile double p = 2.5;
volatile double n = -2.5;
fesetround(FE_TONEAREST);
double rn_p = rint_fn(p), rn_n = rint_fn(n);
fesetround(FE_UPWARD);
double ru_p = rint_fn(p), ru_n = rint_fn(n);
fesetround(FE_DOWNWARD);
double rd_p = rint_fn(p), rd_n = rint_fn(n);
fesetround(FE_TOWARDZERO);
double rz_p = rint_fn(p), rz_n = rint_fn(n);
fesetround(orig_round);
bool c2_pass = (
rn_p == 2.0 && rn_n == -2.0 &&
ru_p == 3.0 && ru_n == -2.0 &&
rd_p == 2.0 && rd_n == -3.0 &&
rz_p == 2.0 && rz_n == -2.0 &&
fegetround() == orig_round
);
fenv_t env_save;
feclearexcept(FE_ALL_EXCEPT);
fegetenv(&env_save);
fesetround(FE_UPWARD);
feraiseexcept(FE_INEXACT);
fesetenv(&env_save);
int env_restored = (fegetround() == orig_round && fetestexcept(FE_INEXACT) == 0) ? 1 : 0;
fenv_t hold_env;
feholdexcept(&hold_env);
volatile double z2 = one / zero;
(void)z2;
feupdateenv(&hold_env);
int propagated = (fetestexcept(FE_DIVBYZERO) != 0) ? 1 : 0;
feclearexcept(FE_ALL_EXCEPT);
bool c3_pass = (env_restored == 1 && propagated == 1);
printf(
"{"
"\"runtime\":\"GCC C11 (mstorsjo/llvm-mingw:20260616)\","
"\"cases\":["
"{\"name\":\"detect_and_manage_fp_exceptions\",\"expected\":\"div=1,inv=1,ov=1\",\"actual\":\"div=%d,inv=%d,ov=%d\",\"passed\":%s},"
"{\"name\":\"rounding_modes_control\",\"expected\":\"near=2/-2,up=3/-2,down=2/-3,zero=2/-2\",\"actual\":\"near=%.0f/%.0f,up=%.0f/%.0f,down=%.0f/%.0f,zero=%.0f/%.0f\",\"passed\":%s},"
"{\"name\":\"fegetenv_fesetenv_and_hold_update\",\"expected\":\"restored=1,propagated=1\",\"actual\":\"restored=%d,propagated=%d\",\"passed\":%s}"
"]"
"}\n",
div_flag, inv_flag, ov_flag, c1_pass ? "true" : "false",
rn_p, rn_n, ru_p, ru_n, rd_p, rd_n, rz_p, rz_n, c2_pass ? "true" : "false",
env_restored, propagated, c3_pass ? "true" : "false"
);
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。
入口文件:projects/c-standard-library-fenv-h-lab/explore-standard-library-fenv-h.c
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-standard-library-fenv-h.c,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3244.zip
课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip