53

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;
}
legends2k
  • 31,634
  • 25
  • 118
  • 222
Eunsu Kim
  • 731
  • 1
  • 5
  • 9
  • M_PI is not actually part of the standard, so no guarantee that it's been defined in math.h – AndyG Sep 26 '14 at 17:48
  • Does this answer your question? [M\_PI works with math.h but not with cmath in Visual Studio](https://stackoverflow.com/questions/6563810/m-pi-works-with-math-h-but-not-with-cmath-in-visual-studio) – Marine Galantin Jan 28 '22 at 18:53

7 Answers7

106

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.

Shep
  • 7,990
  • 8
  • 49
  • 71
  • 5
    i am using Visual Studio 2013 this is why that is not running right? is it running in g++ ? – Eunsu Kim Sep 26 '14 at 21:20
  • Dear Eunsu,if you go inside the head file inside "Exsternal Dependencies" of Visual Studio 2013, you can see that this is part of the code: '#if defined (_USE_MATH_DEFINES) && !defined (_MATH_DEFINES_DEFINED) /* **** */ #define M_PI 3.14159265358979323846 /* **** */ #endif' If you want to use this #define M_PI you MUST define _USE_MATH_DEFINES with an #define. This is the reason! – Leos313 Jun 21 '16 at 13:58
  • 4
    It makes sense but doesn't work for me... I am using VS 2015 Community... – Summer Sun May 24 '17 at 07:38
  • Same with VS2019, it doesn't work. – Patapoom Nov 19 '21 at 08:35
39

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.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Hemant Gangwar
  • 2,172
  • 15
  • 27
15

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>
legends2k
  • 31,634
  • 25
  • 118
  • 222
  • 1
    C++ headers have macros, includes, variables, functions and classes -- declared, defined or both. Now depending on a few flags (lookup _conditional compilation_) the pre-processor includes / excludes them. 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. Hope that answers your question. – legends2k Aug 31 '18 at 07:54
  • 1
    Worked for me under Win10 + Msys2 Portable. – sancho.s ReinstateMonicaCellio Feb 28 '19 at 03:27
14

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>
Metric Crapton
  • 461
  • 5
  • 10
  • 1
    Good advice! I'd alter it slightly to advise putting definitions like that before *all* `#include` directives and not just single out `iostream`. In any case, your explanation is spot-on. I'd give it two up-votes, if allowed. – Mike Housky Jun 16 '22 at 16:18
7

Use this include for Windows 10 (and Windows 11):

#include <corecrt_math_defines.h>
Patapoom
  • 794
  • 1
  • 7
  • 16
  • Note that this won't work if cross-compiling on MSYS. It seems in that case, you'll want to avoid including this file and instead #define USE_MATH_DEFINES. – Tyler Shellberg Aug 08 '22 at 18:02
1

You must use _USE_MATH_DEFINES before other headers like this:

#define _USE_MATH_DEFINES
#include <cmath>
#incude other headers...
0

I used C99 in NetBeans with remote linux host with its build tools.
Try adding #define _GNU_SOURCE and add the -lm during linking.

Eruthon
  • 11
  • 5