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

C++17 离线复现并高精度比对 <numbers> 全部 13 个数学常量、三角恒等式与三浮点精度

CPP本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。explore-cpp-libs-numbers.cpp
explore-cpp-libs-numbers.cpp173 行
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <type_traits>
#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

// C++17 inline constexpr variable templates mirroring C++20 <numbers> (std::numbers)
namespace cpp17_numbers {

template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
inline constexpr T e_v = static_cast<T>(2.718281828459045235360287471352662498L);

template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
inline constexpr T log2e_v = static_cast<T>(1.442695040888963407359924681001892137L);

template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
inline constexpr T log10e_v = static_cast<T>(0.434294481903251827651128918916605082L);

template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
inline constexpr T pi_v = static_cast<T>(3.141592653589793238462643383279502884L);

template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
inline constexpr T inv_pi_v = static_cast<T>(0.318309886183790671537767526745028724L);

template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
inline constexpr T inv_sqrtpi_v = static_cast<T>(0.564189583547756286948079451560772586L);

template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
inline constexpr T ln2_v = static_cast<T>(0.693147180559945309417232121458176568L);

template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
inline constexpr T ln10_v = static_cast<T>(2.302585092994045684017991454684364208L);

template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
inline constexpr T sqrt2_v = static_cast<T>(1.414213562373095048801688724209698079L);

template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
inline constexpr T sqrt3_v = static_cast<T>(1.732050807568877293527446341505872367L);

template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
inline constexpr T inv_sqrt3_v = static_cast<T>(0.577350269189625764509148780501957456L);

template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
inline constexpr T egamma_v = static_cast<T>(0.577215664901532860606512090082402431L);

template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
inline constexpr T phi_v = static_cast<T>(1.618033988749894848204586834365638118L);

inline constexpr double e = e_v<double>;
inline constexpr double log2e = log2e_v<double>;
inline constexpr double log10e = log10e_v<double>;
inline constexpr double pi = pi_v<double>;
inline constexpr double inv_pi = inv_pi_v<double>;
inline constexpr double inv_sqrtpi = inv_sqrtpi_v<double>;
inline constexpr double ln2 = ln2_v<double>;
inline constexpr double ln10 = ln10_v<double>;
inline constexpr double sqrt2 = sqrt2_v<double>;
inline constexpr double sqrt3 = sqrt3_v<double>;
inline constexpr double inv_sqrt3 = inv_sqrt3_v<double>;
inline constexpr double egamma = egamma_v<double>;
inline constexpr double phi = phi_v<double>;

} // namespace cpp17_numbers

int main() {
    std::vector<lab::Case> cases;
    namespace num = cpp17_numbers;

    // Case 1: Verify all 13 constants against <cmath> / harmonic series
    double harmonic = 0.0;
    const int H_N = 10000;
    for (int k = 1; k <= H_N; ++k) harmonic += 1.0 / k;
    double gamma_est = harmonic - std::log(static_cast<double>(H_N)) - 1.0 / (2.0 * H_N);

    bool c1 =
        (std::fabs(num::e - std::exp(1.0)) < 1e-15) &&
        (std::fabs(num::log2e - std::log2(num::e)) < 1e-15) &&
        (std::fabs(num::log10e - std::log10(num::e)) < 1e-15) &&
        (std::fabs(num::pi - std::acos(-1.0)) < 1e-15) &&
        (std::fabs(num::inv_pi - 1.0 / num::pi) < 1e-15) &&
        (std::fabs(num::inv_sqrtpi - 1.0 / std::sqrt(num::pi)) < 1e-15) &&
        (std::fabs(num::ln2 - std::log(2.0)) < 1e-15) &&
        (std::fabs(num::ln10 - std::log(10.0)) < 1e-15) &&
        (std::fabs(num::sqrt2 - std::sqrt(2.0)) < 1e-15) &&
        (std::fabs(num::sqrt3 - std::sqrt(3.0)) < 1e-15) &&
        (std::fabs(num::inv_sqrt3 - 1.0 / std::sqrt(3.0)) < 1e-15) &&
        (std::fabs(num::egamma - gamma_est) < 1e-8) &&
        (std::fabs(num::phi - (1.0 + std::sqrt(5.0)) / 2.0) < 1e-15);
    cases.push_back({"all_13_math_constants_precision", lab::jbool(true), lab::jbool(c1), c1});

    // Case 2: Trigonometric and logarithmic identities
    bool c2 =
        (std::fabs(std::sin(num::pi / 2.0) - 1.0) < 1e-15) &&
        (std::fabs(std::cos(num::pi) + 1.0) < 1e-15) &&
        (std::fabs(std::log(num::e) - 1.0) < 1e-15);
    cases.push_back({"trig_and_log_identities", lab::jbool(true), lab::jbool(c2), c2});

    // Case 3: Circle geometry, exponential e^x, and golden ratio phi
    double r = 5.0;
    double circ = 2.0 * num::pi * r;
    double area = num::pi * std::pow(r, 2.0);
    double exp2_fn = std::exp(2.0);
    double exp2_pow = std::pow(num::e, 2.0);
    double golden_h = 10.0 * num::phi;
    bool c3 =
        (std::fabs(circ - 31.41592653589793) < 1e-12) &&
        (std::fabs(area - 78.53981633974483) < 1e-12) &&
        (std::fabs(exp2_fn - exp2_pow) < 1e-14) &&
        (std::fabs(golden_h - 16.18033988749895) < 1e-12) &&
        (std::fabs(num::phi * num::phi - (num::phi + 1.0)) < 1e-15);
    cases.push_back({"circle_exp_and_golden_ratio_apps", lab::jbool(true), lab::jbool(c3), c3});

    // Case 4: Precision comparison across float, double, and long double
    long double ref_pi = std::acos(-1.0L);
    long double err_f = std::fabs(static_cast<long double>(num::pi_v<float>) - ref_pi);
    long double err_d = std::fabs(static_cast<long double>(num::pi_v<double>) - ref_pi);
    long double err_ld = std::fabs(num::pi_v<long double> - ref_pi);
    bool c4 = (err_ld <= err_d) && (err_d < err_f) && (err_f < 1e-6L) && (err_d < 1e-15L);
    cases.push_back({"float_double_long_double_precision", 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-numbers-lab/explore-cpp-libs-numbers.cpp

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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