M_PI is undefined

I want to use M_PI as the constant for 3.141592blabla, but while Code::Blocks understands it, Microsoft Visual Studio doesn't. I've tried including these:

1
2
#include <cmath>
#include <math.h> 


and it still says: "Error: identifier 'M_PI' is undefined"

Help is appreciated, thanks.
Why should it understand it? It's not a part of math.h or cmath in either C or C++.

that said, Visual Studio has a way to enable it, see http://msdn.microsoft.com/en-us/library/4hwaceh6(v=vs.110).aspx

For a portable solution, see boost: http://www.boost.org/doc/libs/1_53_0/libs/math/doc/sf_and_dist/html/math_toolkit/constants/constants.html
Last edited on
My instructor advised against using #define but I'll go ahead and use it...unless there is another way.
M_PI is non-standard.

Try the following:
1
2
#define _USE_MATH_DEFINES
#include <cmath> 

My instructor advised against using #define


#define is still needed in C++ to control #ifdef conditional compilation by the preprocessor phase of compilation. That said, the need for conditional compilation is certainly a lot less in C++.

What your instructor is advising is that #define are not the proper way to define constants.

Not the C++ way:
1
2
 
#define PI 3.14 


The C++ way:
 
const double PI = 3.14.

Last edited on
Topic archived. No new replies allowed.