YouTip LogoYouTip

Cpp Libs Numbers

Before C++20, we usually wrote mathematical constants like this: ```cpp const double PI = 3.14159265358979323846; The problem is obvious: **Precision is written casually, inconsistent, easy to write wrong, and type-unsafe.** C++20 officially introduces `#include `, providing a standardized set of mathematical constants, with compiler-guaranteed precision and type correctness. `std::numbers` is a standard library module introduced in C++20, mainly used to provide a set of commonly used mathematical constants, a standard mathematical and physical constants repository. `std::numbers` is located in the `` header file and contains many mathematical constants, covering common constants such as pi, the base of natural logarithm, and the golden ratio. Using these constants in C++ can improve code readability, precision, and efficiency, avoiding repeated definitions and manual input of constant values. Advantages of `std::numbers` compared to traditionalapproach: | Dimension | Traditional Writing | `` | | --- | --- | --- | | Precision | Written casually | Standard high precision | | Type | Fixed | Auto-matched | | Readability | General | Semantically clear | | Compile-time constant | Not necessarily | Definitely | | Standardization | Poor | Library-level | ### Constants Included in std::numbers Module The std::numbers namespace contains the following members: ```cpp template inline constexpr T e_v // Natural constant e template inline constexpr T log2e_v // log2e template inline constexpr T log10e_v // log10e template inline constexpr T pi_v // Circumference ratio Ο€ template inline constexpr T inv_pi_v // 1/Ο€ template inline constexpr T inv_sqrtpi_v //1/βˆšΟ€ template inline constexpr T ln2_v // ln2 template inline constexpr T ln10_v // ln10 template inline constexpr T sqrt2_v //√2 template inline constexpr T sqrt3_v //√3 template inline constexpr T inv_sqrt3_v // 1/√3 template inline constexpr T egamma_v // Euler constant Ξ³ template inline constexpr T phi_v // Golden ratio Ξ¦ inline constexpr double e = e_v inline constexpr double log2e = log2e_v inline constexpr double log10e = log10e_v inline constexpr double pi = pi_v inline constexpr double inv_pi = pi_v inline constexpr double inv_sqrtpi = pi_v inline constexpr double ln2 = ln2_v inline constexpr double ln10 = ln10_v inline constexpr double sqrt2 = sqrt2_v inline constexpr double sqrt3 = sqrt3_v inline constexpr double inv_sqrt3 = sqrt3_v inline constexpr double egamma = egamma_v inline constexpr double phi = phi_v The following is the list of constants included in std::numbers: | Constant/Template Name | Description | Example Value (Approximate) | | --- | --- | --- | | `e_v` | Mathematical constant e (base of natural logarithm) | `2.718281828459045` | | `log2e_v` | Logarithm of e with base 2 (logβ‚‚(e)) | `1.4426950408889634` | | `log10e_v` | Logarithm of e with base 10 (log₁₀(e)) | `0.4342944819032518` | | `pi_v` | Mathematical constant Ο€ (circumference ratio) | `3.141592653589793` | | `inv_pi_v` | 1/Ο€ (reciprocal of Ο€) | `0.318309886183121` | | `inv_sqrtpi_v` | 1/βˆšΟ€ (reciprocal
← Vscode ThemesPython Spider Beautifulsoup β†’