• Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
#define _USE_MATH_DEFINES


Dunno why they have done it this way...

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
It was actually meant for math.h... :P

Share this post


Link to post
Share on other sites
Quote:
Original post by Dan Forever
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


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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement