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

This topic is 4275 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?

##### Share on other sites
Yes. Listen to your compiler, because then, it will listen to you.

##### Share on other sites
itoa came first. _itoa is what happens when committees are let loose on things.

There is nothing even vaugely standard about _itoa_s. What happened it that somebody at MS woke up one day and declared that all functions that write to a string buffer, but don't have a parameter specifying the length of the buffer, are evil. So they made a new function. There is some merit to thier position but it's really annoying anyway.

##### Share on other sites
itoa() is not, and was never, a standard function in either C or C++. It's included in many C and C++ compilers as an extension to the standard libraries. Microsoft at some point decided that any non-standard functions included in the standard headers files shipped with MSVC should be replaced with versions with leading underscores, thus itoa() became _itoa().

##### Share on other sites
If you're using C++, you don't want to use anything named similar to itoa() for the purpose of getting a text representation of a number.

If you're just outputting the number to the console, that works implicitly - you have no need to convert to a string first:

int foo = 42;cout << foo << endl;

If you want to store it as text in an object for some reason, you can do the conversion by making use of an object called a stringstream. This is an object that provides a stream interface (you can "read from"/"write to" it as if it were cin, cout or a file stream), but operates upon a string rather than a file or console. The approach is to create an "empty" stringstream, write the int into it, and then copy out the string buffer.

You can construct a complex string in this manner all at once. However, if you just want a single value to be converted to text, you are better off using a wrapper for this process. The Boost library provides an excellent wrapper called 'lexical_cast'. It also does some error checking for you, and lets you (try to!) convert between any two types by using a string representation (a stringstream buffer) as an intermediate: it reads the input into the buffer, then writes it out to the result.

As for the PI constant, please do not use a #define, even if you are defining it yourself. The language allows you to create const identifiers that have a type and which respect scope - and which aren't awful automated-text-cut-and-paste hacks! All you do is 'const double PI = 3.1415926535;'.

##### Share on other sites
Thanks, I'll look into stringstream. The reason why I used itoa was I am working with SDL and wanted to display a number and needed to convert it into a string. I remember someone told me that itoa would work.

As for using #define, is it ok to use it for game states? Right now I use it to define menu 0, intro 1, etc. Is that alright to use #define?

##### Share on other sites
Quote:
 Original post by eektorAs for using #define, is it ok to use it for game states? Right now I use it to define menu 0, intro 1, etc. Is that alright to use #define?
enum {    MENU,    INTRO,    GAME,    QUIT,    MAX_STATES};

##### Share on other sites
Thanks jyk, I'll use an enum instead of the define.

Since I couldn't find any information on stringstream from my book, I want to make sure I got it right.

std::ostringstream strout;
strout << "Computer Score: " << g_comp_score << std::endl;

Then if I were to put
std::cout << strout.str();

It should print out "Computer Score: (whatever number g_comp_score is)", right?

To clear strout, I would have to do:

strout.str("");

Am I correct?