Jump to content
  • Advertisement
Sign in to follow this  
eektor

Is there a function in the standard library for PI?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
#define PI 3.14159265

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!