Standard C++ doesn't have a constant for PI. Many C++ compilers define M_PI in cmath (or in math. h for C) as a non-standard extension. You may have to #define _USE_MATH_DEFINES before you can see it.
Note: Some programs use a constant named PI which has the same value as M_PI . This constant is not standard; it may have appeared in some old AT&T headers, and is mentioned in Stroustrup's book on C++.
Pi is defined as the ratio of the circumference of any circle to its diameter. As all circles are similar and therefore proportional in dimensions, pi is therefore always the same for all circles and is a constant.
The PI constant is present in the cmath header file. The name of the constant is M_PI. We can simply include that header file, and use the constant to perform operation.
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 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
Up to C++20, no, none of the standards introduced the constant that would represent the number pi (π). You can approximate the number in your code:
constexpr double pi = 3.14159265358979323846;
Other languages such as C# have the constant declared in their libraries.
Update:
Starting with the C++20, there indeed is a pi
constant declared inside the <numbers>
header. It is accessed via: std::numbers::pi
.
As others said there is no std::pi
but if you want precise PI
value you can use:
constexpr double pi = std::acos(-1);
This assumes that your C++ implementation produces a correctly-rounded value of PI from acos(-1.0)
, which is common but not guaranteed.
It's not constexpr
, but in practice optimizing compilers like gcc and clang evaluate it at compile time. Declaring it const
is important for the optimizer to do a good job, though.
M_PI
is defined by "a standard", if not a language standard: POSIX with the X/Open System Interfaces extension (which is very commonly supported and required for official UNIX branding).
C++20, meanwhile, does have such constants (merged in the last round of C++20 features). Specifically, there is both std::numbers::pi
(of type double
) and a variable template that you can use if you want a different floating point type, e.g. std::numbers::pi_v<float>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With