Is there a function in the standard library for PI?
(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?
PI in C(++) is a constant (so no function), called M_PI. It's defined in <math.h> or <cmath> as you already guessed.
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.
Edit: Hmm that didn't work. Any possible reasons why M_PI won't work for me? I'm using VS 2005 EE.
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.
#define _USE_MATH_DEFINES
before including cmath.
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).
As the anymous poster posted, your're not going to get any deeper with
3.1415926535897932384626433832795...
especially since floating point values cut so much out.
3.1415926535897932384626433832795...
#define pi 3.141592653589
especially since floating point values cut so much out.
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.
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.
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?
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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement