The C++20 standard introduced a compelling library feature, std::numbers
, aimed at enriching the language’s support for mathematical constants. This addition, part of the <numbers>
header, provides compile-time constants for a variety of mathematical values. These constants offer precision and ease of use that can significantly improve the readability and accuracy of mathematical computations in C++ programs.
std::numbers
The std::numbers
library is a static collection of mathematical constants with varying degrees of precision, depending on the type. It includes well-known constants such as (pi), (Euler’s number), and many others, facilitating their use without the need for manual definition or inclusion of external libraries.
std::numbers
are defined as constexpr
and are template-specialized for different floating-point types, including float
, double
, and long double
. This means that we can use these constants with the precision our application requires.std::numbers
, these constants are readily available and standardized across different compilers and platforms.std::numbers
enhances code readability and reduces the likelihood of errors associated with manually defining and using mathematical constants.The <numbers>
header includes a variety of constants. Here are a few notable examples:
std::numbers::pi
: Represents the π constant, the ratio of the circumference of a circle to its diameter.std::numbers::e
: Represents Euler’s number, the base of natural logarithms.std::numbers::phi
: Represents the golden ratio, an irrational number that appears in various aspects of art, architecture, and nature.std::numbers::sqrt2
: Represents the square root of 2, the length of the diagonal of a square with unit side length.Let’s look at code example demonstrating how to use std::numbers
in a C++ program:
#include <iostream>#include <numbers>int main() {std::cout << "Pi: " << std::numbers::pi << std::endl;std::cout << "Euler's number: " << std::numbers::e << std::endl;std::cout << "Golden ratio: " << std::numbers::phi << std::endl;std::cout << "Square root of 2: " << std::numbers::sqrt2 << std::endl;return 0;}
Lines 1–2: We include two headers: <iostream>
for handling input/output operations, allowing text to be printed to the console, and <numbers>
for accessing pre-defined mathematical constants.
Line 4: We define a main
function as the entry point of the program. It’s where the execution of the program begins.
Line 5: We utilize std::cout
along with std::numbers::pi
to print the value of <numbers>
header.
Line 6: Similar to the previous line, we print Euler’s number (std::numbers::e
. This demonstrates how to access and use Euler’s number from the <numbers>
library.
Line 7: We print the value of the golden ratio (std::numbers::phi
. This line highlights the availability of the golden ratio as a constant in the <numbers>
header.
Line 8: We output the square root of std::numbers::sqrt2
. This is an example of accessing another mathematical constant provided by the <numbers>
header.
Whether we’re working on scientific simulations, graphics rendering, or any other domain where mathematical constants are essential, std::numbers
is a valuable addition to our programming toolkit.
std::numbers
simplifies the development of mathematically intensive applications. For C++ programmers, this means improved code readability, accuracy, and a more streamlined approach to mathematical programming. Whether we’re working on scientific simulations, graphics rendering, or any other domain where mathematical constants are essential, std::numbers
is a valuable addition to our programming toolkit.
Ready to master C++?
Our Learn C++ course will help you build practical skills, from basic loops to complex program structures. Join now and start coding your way to success!
Free Resources