#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <stdbool.h>
static bool near(double a, double b, double tol) {
return fabs(a - b) <= tol;
}
int main(void) {
const double PI = acos(-1.0);
int exp2 = 0;
double mant = frexp(8.0, &exp2);
double rebuilt = ldexp(mant, exp2);
double ipart = 0.0;
double frac = modf(5.75, &ipart);
bool c1_pass = (
near(sqrt(2.0), 1.4142135623730951, 1e-12) &&
near(pow(2.0, 3.0), 8.0, 1e-12) &&
near(log(exp(2.0)), 2.0, 1e-12) &&
near(log10(1000.0), 3.0, 1e-12) &&
near(mant, 0.5, 1e-12) && exp2 == 4 &&
near(rebuilt, 8.0, 1e-12) &&
near(ipart, 5.0, 1e-12) && near(frac, 0.75, 1e-12)
);
bool c2_pass = (
near(sin(PI / 6.0), 0.5, 1e-12) &&
near(cos(PI / 3.0), 0.5, 1e-12) &&
near(atan(1.0), PI / 4.0, 1e-12) &&
near(atan2(1.0, -1.0), 3.0 * PI / 4.0, 1e-12) &&
near(acos(0.5), PI / 3.0, 1e-12) &&
near(asin(0.5), PI / 6.0, 1e-12) &&
near(cosh(0.0), 1.0, 1e-12) &&
near(sinh(0.0), 0.0, 1e-12) &&
near(tanh(0.0), 0.0, 1e-12)
);
double c_val = ceil(5.8);
double f_val = floor(5.8);
double a_val = fabs(-5.8);
double m_val = fmod(5.5, 2.0);
bool c3_pass = (near(c_val, 6.0, 1e-12) && near(f_val, 5.0, 1e-12) && near(a_val, 5.8, 1e-12) && near(m_val, 1.5, 1e-12));
errno = 0;
volatile double big = 1000.0;
double ov = exp(big);
int ov_ok = (ov == HUGE_VAL && errno == ERANGE) ? 1 : 0;
int cls_ok = (
fpclassify(0.0) == FP_ZERO &&
fpclassify(1.5) == FP_NORMAL &&
fpclassify(INFINITY) == FP_INFINITE &&
fpclassify(NAN) == FP_NAN
) ? 1 : 0;
bool c4_pass = (ov_ok == 1 && cls_ok == 1);
printf(
"{"
"\"runtime\":\"GCC C11 (mstorsjo/llvm-mingw:20260616)\","
"\"cases\":["
"{\"name\":\"power_exp_log_decomp\",\"expected\":\"pow=8.0,mant=0.5,exp=4,ipart=5.0,frac=0.75\",\"actual\":\"pow=%.1f,mant=%.1f,exp=%d,ipart=%.1f,frac=%.2f\",\"passed\":%s},"
"{\"name\":\"trig_and_hyperbolic\",\"expected\":1,\"actual\":%d,\"passed\":%s},"
"{\"name\":\"rounding_and_fmod\",\"expected\":\"ceil=6.0,floor=5.0,fabs=5.8,fmod=1.5\",\"actual\":\"ceil=%.1f,floor=%.1f,fabs=%.1f,fmod=%.1f\",\"passed\":%s},"
"{\"name\":\"fpclassify_and_overflow_macros\",\"expected\":\"ov=1,cls=1\",\"actual\":\"ov=%d,cls=%d\",\"passed\":%s}"
"]"
"}\n",
pow(2.0, 3.0), mant, exp2, ipart, frac, c1_pass ? "true" : "false",
c2_pass ? 1 : 0, c2_pass ? "true" : "false",
c_val, f_val, a_val, m_val, c3_pass ? "true" : "false",
ov_ok, cls_ok, c4_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-math-h-lab/explore-standard-library-math-h.c
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-standard-library-math-h.c,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3249.zip
课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip