#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
int k, i, nmax = 1000000;
int out[7] = {1, 10, 100, 1000, 10000, 100000, 1000000};
float sf = 0.0f;
double sd = 0.0;
double exact = M_PI * M_PI / 6.0;
double rf, rd;
FILE *fp;
if ((fp = fopen("basel.csv", "w")) == NULL)
{
printf("cannot open file\n");
exit(-1);
}
fprintf(fp, "n,float,double,rel.err.flt,rel.err.dbl\n");
printf("%8s %24s %24s %16s %16s\n", "i", "float", "double", "rel.err.flt.", "rel.err.dbl.");
i = 0;
for (k = 1; k <= nmax; k++)
{
sf += 1.0f / ((float)k * (float)k);
sd += 1.0 / ((double)k * (double)k);
if (k == out[i]) {
rf = fabs((double)sf - exact) / exact;
rd = fabs(sd - exact) / exact;
printf("%8d: %.18e %.18e %.15e %.15e\n", k, (double)sf, sd, rf, rd);
fprintf(fp, "%d,%.18e,%.18e,%.15e,%.15e\n", k, (double)sf, sd, rf, rd);
i++;
}
}
fclose(fp);
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: