results error with long double type

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.

Hi xll_bit,

Try using “fabsl” for long double as well as the “Lf” print descriptor.

% cat pi.c
#include <stdio.h>
#include <math.h>
#define PI 3.141592653589793238462643
int main(int argc, char *argv[]) {
        int N = 10000;
        long double sum=0.0,x;
        for(int i=0;i<N;i++){
                x = (long double)(i-0.5)/(long double)N;
                sum+=4.0/(1+x*x)/(long double)N;
        }
                printf("pi is %Lf,error is %.16Lf\n",
                        sum,fabsl(sum-PI));
}
% pgcc pi.c
% a.out
pi is 3.141793,error is 0.0001999908320835

Hope this helps,
Mat