What is C++ std::numbers?
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.
Introduction to 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.
Key features and benefits
- Precision: The constants in
std::numbersare defined asconstexprand are template-specialized for different floating-point types, includingfloat,double, andlong double. This means that we can use these constants with the precision our application requires. - Convenience: Prior to C++20, programmers often defined these constants themselves or relied on less standardized ways of accessing them. With
std::numbers, these constants are readily available and standardized across different compilers and platforms. - Type safety and readability: Using
std::numbersenhances code readability and reduces the likelihood of errors associated with manually defining and using mathematical constants.
Available 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.
Code example
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;}
Code explanation
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
mainfunction as the entry point of the program. It’s where the execution of the program begins.Line 5: We utilize
std::coutalong withstd::numbers::pito print the value of(pi) to the console. This line showcases the use of the constant from the <numbers>header.Line 6: Similar to the previous line, we print Euler’s number (
) to the console using 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 (
) using 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
to the console, utilizing std::numbers::sqrt2. This is an example of accessing another mathematical constant provided by the<numbers>header.
Application
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