• Create Account

## Math.h

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1Dan Forever  Members

150
Like
0Likes
Like

Posted 12 February 2005 - 01:12 AM

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 :(

### #2Rob Loach  Members

1504
Like
0Likes
Like

Posted 12 February 2005 - 01:21 AM

#include <cmath> for C++.

Rob Loach [Website] [Projects] [Contact]

### #3RigidBody  Members

176
Like
0Likes
Like

Posted 12 February 2005 - 01:30 AM

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.

### #4doynax  Members

850
Like
0Likes
Like

Posted 12 February 2005 - 01:34 AM

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

### #5 Anonymous Poster_Anonymous Poster_*   Guests

0Likes

Posted 12 February 2005 - 01:38 AM

#define _USE_MATH_DEFINES

Dunno why they have done it this way...

### #6Dan Forever  Members

150
Like
0Likes
Like

Posted 12 February 2005 - 01:55 AM

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

### #7Dan Forever  Members

150
Like
0Likes
Like

Posted 12 February 2005 - 01:59 AM

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

### #8 Anonymous Poster_Anonymous Poster_*   Guests

0Likes

Posted 12 February 2005 - 02:40 AM

It was actually meant for math.h... :P

### #9Dan Forever  Members

150
Like
0Likes
Like

Posted 12 February 2005 - 02:44 AM

cmath includes math.h anyway :P

### #10RigidBody  Members

176
Like
0Likes
Like

Posted 12 February 2005 - 02:46 AM

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.

### #11dalleboy  Members

324
Like
0Likes
Like

Posted 12 February 2005 - 03:10 AM

An though it does in VC.NET 2003.

Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.