Reputation: 23
I'm writing this code and a warning appear when I tried to compile.
#include <stdio.h>
#include <math.h>
#define EPS 1.5e-6
#define M_PI 3.14159265358979
int main()
{
double x1,x2,xm,y1,y2,ym;
int m;
for(m=0;m<11;m++){
x1=1.450;
x2=1.489;
y1=atan(pow(x1*x1-1.5*1.5,0.5)/pow(1.489*1.489-x1*x1,0.5))\
+ atan(pow(x1*x1-1.450*1.450,0.5)/pow(1.489*1.489-x1*x1,0.5))\
- 4.5 * EPS * ((2 * M_PI)/(1.5 * EPS)) * pow(1.489*1.489-x1*x1,0.5)\
+ m*M_PI*1.5;
y2=atan(pow(x2*x2-1.5*1.5,0.5)/pow(1.489*1.489-x2*x2,0.5))\
+ atan(pow(x2*x2-1.450*1.450,0.5)/pow(1.489*1.489-x2*x2,0.5))\
- 4.5 * EPS * ((2 * M_PI)/(1.5 * EPS)) * pow(1.489*1.489-x2*x2,0.5)\
+ m*M_PI*1.5;
if(y1*y2>0){
printf("change initial values\n");
}
else{
while(fabs(x1-x2)>EPS){
xm=(x1+x2)/2;
ym=atan(pow(xm*xm-1.5*1.5,0.5)/pow(1.489*1.489-xm*xm,0.5))\
+ atan(pow(xm*xm-1.450*1.450,0.5)/pow(1.489*1.489-xm*xm,0.5))\
- 4.5 * EPS * ((2 * M_PI)/(1.5 * EPS)) * pow(1.489*1.489-xm*xm,0.5)\
+ m*M_PI*1.5;
if(y1*ym>0){
x1=xm;
}
else{
x2=xm;
}
}
printf("n[%d] = %.9f;\n",m, xm);
}
}
return 0; }
The warning is :
warning: 'M_PI' macro redefined [-Wmacro-redefined]
I can't figure out how to make the warning disappear
Upvotes: 1
Views: 23270
Reputation: 121387
POSIX defines a macro M_PI
as the value of Pi as an extension to C standard. So, if you are on POSIX system, you don't need to define your own M_PI
.
But if you don't want to then you can compile only in standard C mode such as:
gcc -Wall -Wextra -std=c11 file.c
Upvotes: 1
Reputation: 477020
If there's a possibility that a macro you want to use has already been defined (and presumably defined to a useful value), then you can simply check for that:
#ifndef M_PI
# define M_PI my_value_here
#endif
Or, if you don't trust the existing value, you can abort translation in that case:
#ifdef M_PI
# error Macro M_PI must not be defined
#else
# define M_PI my_value_here
#endif
It seems like the GNU C library defines those macros (conditionally) because the "Unix98 standard" requires them.
Upvotes: 6