value of pi


Is there a function in C++ library that tells us the value of pi
It's not in the standard. Some implementations choose to put a #define of PI or M_PI in cmath or math.h, sometimes requiring you to specify #define _USE_MATH_DEFINES yourself.

It's safer to just create it yourself as a const.
double pi == 3.1415926535897;
Last edited on
double pi = 3.1415926535897;, surely?

const double pi = 3.1415926535897;
When I did my very first programming course, it was suggested to use 4*atan(1). This works in any programming language and gives PI to the precision of the particular machine.

The main drawback is that the result is assigned to a variable, not a const.

Mostly I tend to hard-code it as a const as recommended above, using a value such as 3.14159265358979323846264338328, or one of the following:

const long double PI = 3.141592653589793238L;
const double PI = 3.141592653589793;
const float PI = 3.1415927;
Last edited on
Topic archived. No new replies allowed.