• 10
• 10
• 12
• 12
• 14

# Math.h

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

## Recommended Posts

Using Math.h in a simple 2D game, I am able to use functions like sin() and sqrt() without problems, but when trying to use the #defines created in the math.h header file I get the following errors: error C2065: 'M_PI' : undeclared identifier error C3861: 'M_PI': identifier not found, even with argument-dependent lookup math.h is included in a class .h file, but not the .cpp. I find copying the #defines from the math.h file to my class.h solves the problem, but it feels like I'm just working around a problem rather then solving it. I know it's probably something simple but I don't know what it is, as it seems like it should work. Hell, the text editor (VS.net 2003) pops up a tooltip with #define 3.14159... when I hover the mouse over it, and yet the compiler can't find it :(

##### Share on other sites
#include <cmath> for C++.

##### Share on other sites
math.h is a valid c++ header (although it is deprecated), you will need to post the code in order to get a helpful response.

##### Share on other sites
It's not standard in C, I don't know about C++ though.
However it's easy to workaround by defining it manually. Just place the following code in a header together with any other portability fixes.
#ifndef M_PI#define M_PI 3.1415926535897932384626433832795#endif

##### Share on other sites
#define _USE_MATH_DEFINES

Dunno why they have done it this way...

##### Share on other sites
cmath doesn't fix the problem.

#include <cmath>int main()	{	double x = sqrt(14.0);	double y = sin(2.0);	double z = M_PI/2;	return 0;	}

test.cpp(7): error C2065: 'M_PI' : undeclared identifier

##### Share on other sites
ah thanks to the anonymous dude, the solution is this:

#define _USE_MATH_DEFINES#include <cmath>int main()	{	double x = sqrt(14.0);	double y = sin(2.0);	double z = M_PI/2;	return 0;	}

the #define must come before the #include.
Thanks guys

##### Share on other sites
It was actually meant for math.h... :P

##### Share on other sites
cmath includes math.h anyway :P

##### Share on other sites
Quote:
 Original post by Dan Foreverah thanks to the anonymous dude, the solution is this:#define _USE_MATH_DEFINES#include int main() { double x = sqrt(14.0); double y = sin(2.0); double z = M_PI/2; return 0; }the #define must come before the #include.Thanks guys

If you use the cmath header as you did in your example, then you must qualify the std namespace. std::sqrt and std::sin, or using namespace std. Your code as you pasted it should not compile.