C++

Does C11 14 17 or 20 introduce a standard constant for pi

27 September 2026 · 5 min read

Does C11 14 17 or 20 introduce a standard constant for pi

For decades, C++ developers have sought a truly standard, high-precision constant for the mathematical value of pi (π). While the concept of pi is fundamental in countless computations, from geometry to physics simulations, its direct availability within the C++ standard library has been a surprisingly convoluted topic. Many programmers have relied on non-standard macros or crafted their own constants, often leading to inconsistencies and potential precision issues across different projects and compilers. This article delves into the evolution of C++ standards to answer a crucial question: Does C++11, 14, 17 or 20 introduce a standard constant for pi? We’ll explore the historical context, examine the contributions (or lack thereof) of various C++ versions, and highlight the definitive solution offered by modern C++.

The Historical Approach to Pi in C++

Before recent C++ standards, obtaining a precise value for pi typically involved workarounds. The most common non-standard approach was to use the M_PI macro, which is often found in the <cmath> header on POSIX-compliant systems. While convenient, relying on M_PI presented a significant portability challenge, as its availability was not guaranteed by the C++ standard itself. Developers often encountered compilation errors on systems where this macro was undefined, forcing them to implement conditional compilation or seek alternatives.

Another prevalent method involved calculating pi using mathematical functions. Expressions like 4 atan(1.0) or acos(-1.0) were frequently employed. These methods, while standard-compliant, could introduce a slight computational overhead at runtime and might not always yield the highest possible floating-point precision available to a literal constant, depending on the compiler and optimization settings. Furthermore, they made code less readable, as the intent to use pi was obscured by a mathematical operation rather than a direct constant.

Many projects opted for defining their own constants, such as const double PI = 3.14159265358979323846;. While this provides local control over precision and ensures availability, it fragments the ecosystem. Every new project or library might use a slightly different precision or even a different variable name, hindering interoperability and potentially introducing subtle bugs when combining codebases. The lack of a universally recognized, standard C++ pi constant was a persistent pain point for developers striving for robust and portable numerical code.

C++11, C++14, and the Absence of a Standard Pi Constant

When C++11 arrived, bringing with it a wealth of new features like lambdas, auto, and move semantics, many hoped for the inclusion of fundamental mathematical constants. Despite significant advancements in the standard library, C++11 did not introduce a standard constant for pi. The focus of C++11 was largely on core language features and foundational library components that addressed common programming paradigms and efficiency concerns. Mathematical constants, while important, were not prioritized in that standard.

Similarly, C++14, which built upon C++11 with smaller, incremental improvements, also did not address the need for a standard pi constant. Its enhancements were primarily aimed at refining existing features and adding minor conveniences, such as generalized lambda captures and variable templates. The omission in both these standards highlighted a gap that the community continued to voice. Developers continued to rely on the previously mentioned workarounds, underscoring the ongoing demand for a more elegant and standard solution.

The absence of a standard C++ pi constant in these versions meant that projects requiring high-precision calculations still had to manage their own definitions or use system-specific macros. Question & Answer :

There is a rather silly problem with the number pi in C and C++. As far as I know M_PI defined in math.h is not required by any standard.

New C++ standards introduced a lot of complicated math in the standard library - hyperbolic functions, std::hermite and std::cyl_bessel_i, different random number generators and so on and so forth.

Did any of the ’new’ standards bring in a constant for pi? If not - why? How does all this complicated math work without it?

I am aware of similar questions about pi in C++ (they are several years and standards old); I would like to know the current state of the problem.

I am also very interested in why oh why C++ still doesn’t have a pi constant but has a lot of more complicated math.

I know that I can define pi myself as 4*atan(1) or acos(-1) or double pi = 3.14;. Sure. But why do I still have to do it? How do standard math functions work without pi?

Up to and including C++17 pi is not a constant introduced into the language, and it’s a pain in the neck.

I’m fortunate in that I use boost and they define pi with a sufficiently large number of decimal places for even a 128 bit long double.

If you don’t use Boost then hardcode it yourself. Defining it with a trigonometric function is tempting but if you do that you can’t then make it a constexpr. The accuracy of the trigonometric functions is also not guaranteed by any standard I know of (cf. std::sqrt), so really you are on dangerous ground indeed relying on such a function.

There is a way of getting a constexpr value for pi using metaprogramming: see https://web.archive.org/web/20200629174939/http://timmurphy.org/2013/06/27/template-metaprogramming-in-c/


From C++20 some good news. There is a defininition for pi. C++20 adds some mathematical constants in <numbers>. For example std::numbers::pi is a double type.

Reference: https://en.cppreference.com/w/cpp/numeric/constants