The following is the code. When I defined the “sum” using long double, the results became error. However, when I defined the “sum” using double, the result is good.
Besides, I compiled the code with pgcc.
I wants to compute PI using this code.
#include <stdio.h>
#include <math.h>
#define PI 3.141592653589793238462643
int main(int argc, char *argv[]) {
int N = 10000000;
double sum=0.0,x;
for(int i=0;i<N;i++){
x = (i-0.5)/N;
sum+=4.0/(1+x*x)/N;
}
printf("pi is %f,error is %.16f\n",
sum,fabs(sum-PI));
}
I don’t know where is wrong.