std::numbers

std::numbers#

C++20 introduced mathematical constants via the <numbers> header.

For example, we can compute the area of a circle, using the value of \(\pi\) from this library:

Listing 9 circle.cpp#
#include <iostream>
#include <cmath>
#include <numbers>

int main() {

    double r{2.0};

    // we'll compute r**2 as r * r
    double area = std::numbers::pi * r * r;

    std::cout << "area = " << area << std::endl;

}