Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

M_PI flagged as undeclared identifier

When I compile the code below, I got these error messages:

(Error  1   error C2065: 'M_PI' : undeclared identifier 
2   IntelliSense: identifier "M_PI" is undefined)

What is this?

#include <iostream>
#include <math.h>

using namespace std;

double my_sqrt1( double n );`enter code here`

int main() {
double k[5] = {-100, -10, -1, 10, 100};
int i;

for ( i = 0; i < 5; i++ ) {
    double val = M_PI * pow( 10.0, k[i] );
    cout << "n: "
         << val
         << "\tmysqrt: "
         << my_sqrt1(val)
         << "\tsqrt: "
         << sqrt(val)
         << endl;
}

return 0;
}

double my_sqrt1( double n ) {
int i;
double x = 1;


for ( i = 0; i < 10; i++ ) {
    x = ( x + n / x ) / 2;
}

return x;
}
like image 567
Eunsu Kim Avatar asked Sep 26 '14 17:09

Eunsu Kim


People also ask

Why is 'm_pi_2' undeclared?

'M_PI_2' undeclared identifier. After some digging around, I have found the reason which is specific to Visual Studio. The math constants are not defined by default in it, see this post.

Why doesn't GCC include M_pi in its header?

Strictly speaking, the C++ standard doesn't mandate M_PI and hence GCC's header defaults to not including its definition. If someone needs it, they've to ask for it by undefining the guard that excludes it i.e. __STRICT_ANSI__. You can search your compiler's math.h (original C header of cmath) for M_PI and you'd see the guards surrounding it.

How do I find the M_pi in C?

You can search your compiler's math.h (original C header of cmath) for M_PI and you'd see the guards surrounding it. Hope that answers your question. Worked for me under Win10 + Msys2 Portable. However you also include iostream. iostream includes a lot of stuff and one of those things eventually includes cmath.

How to get Pi to print when compiled?

Compiles and prints pi like is should: cl /O2 main.cpp /link /out:test.exe. There must be a mismatch in the code you have posted and the one you're trying to compile. Be sure there are no precompiled headers being pulled in before your #define.


4 Answers

It sounds like you're using MS stuff, according to their docs

Math Constants are not defined in Standard C/C++. To use them, you must first define _USE_MATH_DEFINES and then include cmath or math.h.

So you need something like

#define _USE_MATH_DEFINES
#include <cmath>

as a header.

like image 163
Shep Avatar answered Oct 09 '22 03:10

Shep


math.h does not define M_PI by default.

So go with this:

#ifndef M_PI
    #define M_PI 3.14159265358979323846
#endif

This will handle both cases either your header have M_PI defined or not.

like image 32
Hemant Gangwar Avatar answered Oct 09 '22 03:10

Hemant Gangwar


M_PI is supported by GCC too, but you've to do some work to get it

#undef __STRICT_ANSI__
#include <cmath>

or if you don't like to pollute your source file, then do

g++ -U__STRICT_ANSI__ <other options>
like image 22
legends2k Avatar answered Oct 09 '22 05:10

legends2k


As noted by shep above you need something like

#define _USE_MATH_DEFINES
#include <cmath>

However you also include iostream.

iostream includes a lot of stuff and one of those things eventually includes cmath. This means that by the time you include it in your file all the symbols have already been defined so it is effectively ignored when you include it and the #define _USE_MATH_DEFINES doesn't work

If you include cmath before iostream it should give you the higher precision constants like M_PI

#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
like image 40
Metric Crapton Avatar answered Oct 09 '22 04:10

Metric Crapton