Skip to main content
7 of 13
added 3 characters in body
Ciro Santilli OurBigBook.com
  • 392.6k
  • 120
  • 1.3k
  • 1.1k

C++20 std::numbers::pi

At last, it has arrived: http://eel.is/c++draft/numbers

I expect the usage to be like:

#include <numbers> #include <iostream> int main() { std::cout << std::numbers::pi << std::endl; } 

I'll give it a try when support arrives to GCC, GCC 9.1.0 with g++-9 -std=c++2a still doesn't support it.

The accepted proposal describes:

5.0. “Headers” [headers] In the table [tab:cpp.library.headers], a new <math> header needs to be added.

[...]

namespace std { 
namespace math { template<typename T > inline constexpr T pi_v = unspecified; inline constexpr double pi = pi_v<double>; 

There is also a std::numbers::e of course :-) How to calculate Euler constant or Euler powered in C++?

These constants use the C++14 variable template feature: C++14 Variable Templates: what is their purpose? Any usage example?

In earlier versions of the draft, the constant was under std::math::pi: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0631r7.pdf

Ciro Santilli OurBigBook.com
  • 392.6k
  • 120
  • 1.3k
  • 1.1k