What should be my macro for PI value

Just curious on the correct use of pi value. I have read at many places that tan inverse(1) * 4 would be most accurate, like

#define PI (4.0*atan(1.0))

or should I just copy and paste the well known pi value for 30 odd decimal places as a const long double directly from wiki.

One thing I know for sure is that the fraction 22/7 is not that accurate estimate of pi.

In surveying several of the top #include files, I think most people just dump out a ton of digits (at least enough to more than fully-specify the 15-17 significant digits of a double-precision number) to avoid the trig function calculation every time it is referenced. For example:

#define PI 3.14159265358979323846264338327950288419716939937510582
#define E 2.71828182845904523536028747135266249775724709369995957

If you want macro for PI than use explicit value as @m1sterzer0 suggested, otherwise each time you use your macro you will calculate this trig function and it may cause TLE in some cases.

Actually, the general suggestion is to not use macros at all :slight_smile:

The better way is to have something like:

const double PI = 2 * acos(0.0);
2 Likes

In c++, You can also use M_PI defined in math.h (works with GCC)

1 Like