#include <cfloat>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
namespace lab {
struct Case {
std::string name;
std::string expected_json;
std::string actual_json;
bool passed;
};
std::string esc(const std::string& s) {
std::string out = "\"";
for (unsigned char c : s) {
switch (c) {
case '\"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (c < 0x20) {
char buf[8];
std::snprintf(buf, sizeof(buf), "\\u%04x", c);
out += buf;
} else {
out += static_cast<char>(c);
}
}
}
out += "\"";
return out;
}
std::string jbool(bool v) { return v ? "true" : "false"; }
void emit(const std::vector<Case>& cases) {
std::cout << "{\"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [";
for (std::size_t i = 0; i < cases.size(); ++i) {
if (i) std::cout << ", ";
std::cout << "{\"name\": " << esc(cases[i].name)
<< ", \"expected\": " << cases[i].expected_json
<< ", \"actual\": " << cases[i].actual_json
<< ", \"passed\": " << jbool(cases[i].passed) << "}";
}
std::cout << "]}\n";
}
} // namespace lab
int main() {
std::vector<lab::Case> cases;
// Case 1: float / double / long double range and significant decimal digits
bool c1 =
(sizeof(float) == 4) && (sizeof(double) == 8) && (sizeof(long double) >= 8) &&
(FLT_MIN == std::numeric_limits<float>::min()) &&
(FLT_MAX == std::numeric_limits<float>::max()) &&
(FLT_DIG == 6) &&
(DBL_MIN == std::numeric_limits<double>::min()) &&
(DBL_MAX == std::numeric_limits<double>::max()) &&
(DBL_DIG == 15) &&
(LDBL_MIN == std::numeric_limits<long double>::min()) &&
(LDBL_MAX == std::numeric_limits<long double>::max()) &&
(LDBL_DIG >= 15);
cases.push_back({"float_double_ldbl_range_and_digits", lab::jbool(true), lab::jbool(c1), c1});
// Case 2: Binary exponent limits and machine epsilon semantics
volatile float vf1 = 1.0f;
volatile double vd1 = 1.0;
bool c2 =
(FLT_MIN_EXP == -125) && (FLT_MAX_EXP == 128) &&
(DBL_MIN_EXP == -1021) && (DBL_MAX_EXP == 1024) &&
(LDBL_MIN_EXP <= DBL_MIN_EXP) && (LDBL_MAX_EXP >= DBL_MAX_EXP) &&
(vf1 + FLT_EPSILON > 1.0f) && (vf1 + FLT_EPSILON / 2.0f == 1.0f) &&
(vd1 + DBL_EPSILON > 1.0) && (vd1 + DBL_EPSILON / 2.0 == 1.0) &&
(LDBL_EPSILON <= DBL_EPSILON);
cases.push_back({"exponent_and_machine_epsilon", lab::jbool(true), lab::jbool(c2), c2});
// Case 3: <cmath> floating-point operations (sqrt, pow, sin, cos, tan)
double angle = 0.7;
double s = std::sin(angle);
double c = std::cos(angle);
double t = std::tan(angle);
bool c3 =
(std::sqrt(9.0) == 3.0) &&
(std::pow(2.0, 3.0) == 8.0) &&
(std::fabs(s * s + c * c - 1.0) < 1e-14) &&
(std::fabs(t - (s / c)) < 1e-14);
cases.push_back({"cmath_float_operations", lab::jbool(true), lab::jbool(c3), c3});
// Case 4: Floating-point rounding and epsilon-tolerant comparison
volatile double a = 0.1;
volatile double b = 0.2;
double sum = a + b;
bool exact_eq = (sum == 0.3);
bool tol_eq = (std::fabs(sum - 0.3) <= 4.0 * DBL_EPSILON);
bool c4 = (!exact_eq) && tol_eq;
cases.push_back({"epsilon_tolerant_float_comparison", lab::jbool(true), lab::jbool(c4), c4});
lab::emit(cases);
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。
入口文件:projects/cplusplus-cpp-libs-cfloat-lab/explore-cpp-libs-cfloat.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-libs-cfloat.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2334.zip