蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测 <cmath> 基本算术与指数对数、三角双曲、舍入拆分及浮点状态/域错误 NaN

CPP本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。explore-cpp-libs-cmath.cpp
explore-cpp-libs-cmath.cpp156 行
#include <cmath>
#include <cstdio>
#include <cstdlib>
#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 '\b': out += "\\b"; break;
            case '\f': out += "\\f"; 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"; }

bool near(double a, double b, double eps = 1e-6) {
    return std::fabs(a - b) <= eps;
}

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: basic math + exp/log/power/root + long double suffix
    bool ok1 = (std::abs(-5) == 5)
        && lab::near(std::fabs(-5.5), 5.5)
        && lab::near(std::fmod(5.3, 2.0), 1.3)
        && lab::near(std::remainder(5.5, 2.0), -0.5)
        && lab::near(std::fmax(3.5, 4.2), 4.2)
        && lab::near(std::fmin(3.5, 4.2), 3.5)
        && lab::near(std::hypot(3.0, 4.0), 5.0)
        && lab::near(std::exp(1.0), 2.718281828)
        && lab::near(std::log(std::exp(1.0)), 1.0)
        && lab::near(std::log10(100.0), 2.0)
        && lab::near(std::pow(2, 3), 8.0)
        && lab::near(std::sqrt(16.0), 4.0)
        && lab::near(std::cbrt(27.0), 3.0)
        && lab::near(std::expm1(1.0), std::exp(1.0) - 1.0)
        && lab::near(std::log1p(1e-5), 9.99995e-6, 1e-9)
        && lab::near(static_cast<double>(std::sqrt(9.0L)), 3.0)
        && lab::near(static_cast<double>(sqrtl(9.0L)), 3.0);
    cases.push_back({
        "cmath_basic_and_exp_log",
        lab::jbool(true),
        lab::jbool(ok1),
        ok1
    });

    // Case 2: trigonometric and hyperbolic functions
    const double pi = std::acos(-1.0);
    bool ok2 = lab::near(std::sin(pi / 2.0), 1.0)
        && lab::near(std::cos(pi), -1.0)
        && lab::near(std::tan(0.0), 0.0)
        && lab::near(std::asin(1.0), pi / 2.0)
        && lab::near(std::acos(-1.0), pi)
        && lab::near(std::atan(1.0), pi / 4.0)
        && lab::near(std::atan2(1.0, 1.0), pi / 4.0)
        && lab::near(std::sinh(0.0), 0.0)
        && lab::near(std::cosh(0.0), 1.0)
        && lab::near(std::tanh(1.0), 0.761594156)
        && lab::near(std::asinh(1.0), 0.881373587)
        && lab::near(std::acosh(1.0), 0.0)
        && lab::near(std::atanh(0.5), 0.549306144);
    cases.push_back({
        "cmath_trig_and_hyperbolic",
        lab::jbool(true),
        lab::jbool(ok2),
        ok2
    });

    // Case 3: rounding and modf
    double intpart = 0.0;
    double fracpart = std::modf(2.3, &intpart);
    bool ok3 = lab::near(std::ceil(2.3), 3.0)
        && lab::near(std::floor(2.3), 2.0)
        && lab::near(std::trunc(2.8), 2.0)
        && lab::near(std::round(2.5), 3.0)
        && (std::lround(2.5) == 3L)
        && (std::llround(2.5) == 3LL)
        && lab::near(std::nearbyint(2.5), 2.0)
        && lab::near(std::rint(2.5), 2.0)
        && lab::near(intpart, 2.0)
        && lab::near(fracpart, 0.3);
    cases.push_back({
        "cmath_rounding_and_modf",
        lab::jbool(true),
        lab::jbool(ok3),
        ok3
    });

    // Case 4: floating-point classification and domain NaN (no C++ exception thrown)
    double pos_inf = std::numeric_limits<double>::infinity();
    double qnan = std::numeric_limits<double>::quiet_NaN();
    double neg_sqrt = std::sqrt(-1.0);
    bool ok4 = std::isfinite(3.0)
        && !std::isfinite(pos_inf)
        && std::isinf(pos_inf)
        && std::isnan(qnan)
        && std::isnan(neg_sqrt)
        && std::isnormal(1.0)
        && !std::isnormal(0.0)
        && std::signbit(-5.3)
        && !std::signbit(5.3);
    cases.push_back({
        "cmath_float_classify_and_domain",
        lab::jbool(true),
        lab::jbool(ok4),
        ok4
    });

    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-cmath-lab/explore-cpp-libs-cmath.cpp

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-cpp-libs-cmath.cpp,或下载右侧课程工程包。
  2. 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:C++

课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip

下载课程关联工程包:C++

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2337.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习