value of pi

Oct 30, 2012 at 8:02pm
Hi,

Is there a function in C++ library that tells us the value of pi
Oct 30, 2012 at 8:06pm
No
Oct 30, 2012 at 8:08pm
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.
Oct 30, 2012 at 8:15pm
double pi == 3.1415926535897;
Last edited on Oct 30, 2012 at 8:15pm
Oct 30, 2012 at 8:17pm
double pi = 3.1415926535897;, surely?

Oct 30, 2012 at 8:25pm
const double pi = 3.1415926535897;
Oct 30, 2012 at 9:48pm
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:

1
2
3
const long double PI = 3.141592653589793238L;
const double PI = 3.141592653589793;
const float PI = 3.1415927;
Last edited on Oct 30, 2012 at 10:08pm
Topic archived. No new replies allowed.