DEV Community

yiqi huang
yiqi huang

Posted on Edited on

UniversalConstants: My first C++17 library with NAME(digits) syntax & dual-mode support

Hi everyone! I'm excited (and a bit nervous) to share UniversalConstants (v1.0.0), my very first open-source C++ library.

Motivation

I got tired of repeatedly defining PI across different files or fighting dependency hell when switching between quick prototypes and high-precision scientific calculations. So I built a C++17 library with a unified "Parentheses Precision Syntax" NAME(digits) and Dual-Mode Support:

  1. Lightweight Header-Only Mode (Zero Dependencies): Just drop BasicConstants.h into your project for standard double-precision constants (BasicConstants::PI, BasicConstants::C, etc.).
  2. GMP Hardcore Mode (Arbitrary Precision): Powered by GMP, you specify the exact decimal digits you need inside parentheses:
#include <iostream>
#include "UniversalConstants.h"

using namespace UniversalConstants::Math;
using namespace UniversalConstants::Physics;

int main() {
    // Computes 3.6 million digits of Pi on the fly using Chudnovsky binary splitting
    mpf_class huge_pi = PI(3600000); 

    // Lossless cross-domain physics arithmetic
    mpf_class qg = (H_BAR(50) * G(50)) / (C(50) * C(50) * C(50));
    std::cout << "Result: " << qg << std::endl;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

(Note: For physical constants, values are aligned with CODATA standards and automatically zero-padded if requested digits exceed current experimental limits.)

Looking for Feedback!

Since this is my first C++ library, I know there might be rough edges, subtle bugs, or non-idiomatic CMake/C++ practices. I would genuinely appreciate any code reviews, bug reports, or architectural advice from the community! If you find the concept interesting, a ⭐ on GitHub would also mean a lot to me. Thanks for reading!

Top comments (0)