Public Group

# Is there a function in the standard library for PI?

This topic is 4395 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

(Using C++) I'm trying to clean up my code a bit, and I tried to put PI() in place of 3.14159 (I guess I'm used to working with spreadsheets) and it didn't work. I included cmath already. If there isn't should I define it at the beginning or just leave the number in place in the code?

##### Share on other sites
PI in C(++) is a constant (so no function), called M_PI. It's defined in <math.h> or <cmath> as you already guessed.

##### Share on other sites
I think PI is defined as the constant M_PI.

##### Share on other sites
#define PI 3.14159265

##### Share on other sites
Thank you for the quick replies.

Edit: Hmm that didn't work. Any possible reasons why M_PI won't work for me? I'm using VS 2005 EE.

##### Share on other sites
Mmmm, yep... for some reason unknown to me, Microsoft compilers won't define M_PI until you do:
#define _USE_MATH_DEFINES
before including cmath.

##### Share on other sites
If you poke around in the MSVC++ 2005 headers you'll see the various maths constants (like M_PI) need _USE_MATH_DEFINES defined before you include cmath. This is done because aparently these constants are not part of the standard (I read that in a comment in one of the headers).

##### Share on other sites
As the anymous poster posted, your're not going to get any deeper with

3.1415926535897932384626433832795...

#define pi 3.141592653589

especially since floating point values cut so much out.

##### Share on other sites
M_PI and its ilk aren't actually part of the C standard, which explains the need for the #defines.

Regardless, its better that headers use as few macros as possible by default (especially for things like pi...and min and max) to minimize collisions and weirdness.

##### Share on other sites
Ok, thanks. Since its not part of the C++ standard, I guess I'll just do a regular define.

I have two more questions. I got a warning for using itoa. The warning told me itoa is not part of the standard anymore (?) and to use _itoa. When I changed it, it told me I need to use _itoa_s (why didn't it say that the first time?). Anyway my question is whether I should continue using _itoa_s because that is the standard. I was using the Dev C++ IDE before and had no problems with using just itoa.

Another question is I got a warning for something I didn't even write. It had to do with code in the include file xlocnum. Has anyone had this warning happen to them?

• 10
• 10
• 9
• 48
• 12
• ### Forum Statistics

• Total Topics
631388
• Total Posts
2999717
×