use math constants in visual c++. net 2003

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to use the constant M_PI. So, I insert the directive # include <math.h>.
However when compiled, there are errors : M_PI undeclared identifier.

Second, I can not use the function round(double x).
Anyone could be so kind to help me.
Thanks a lot
 
I need to use the constant M_PI. So, I insert the directive # include <math.h>.
However when compiled, there are errors : M_PI undeclared identifier.

Second, I can not use the function round(double x).
Anyone could be so kind to help me.
Thanks a lot

From math.h:

"Define _USE_MATH_DEFINES before including math.h to expose these
macro definitions for common math constants. These are placed under
an #ifdef since these commonly-defined names are not part of the C/C++
standards."

So:

#define _USE_MATH_DEFINES
#include <math.h>

round() is not a C standard library function. You can create your own
simply:

double round(double x)
{
return floor(x + .5);
}

Or, if you always want to round toward zero:

double round(double x)
{
return x < 0 ? -floor(fabs(x) + .5) : floor(x + .5);
}
 
Back
Top