Last active
December 19, 2015 15:48
-
-
Save bongtrop/5978552 to your computer and use it in GitHub Desktop.
Check Error Numer Subject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() { | |
float fsum1, fsum2; | |
double dsum1, dsum2; | |
int i,n; | |
printf ("%5s%11s%11s%11s%11s%15s%15s\n","N","Float S1","Float S2","Double S1","Double S2","Error S1","Error S2"); | |
for (n=1000;n<=10000;n+=1000) { | |
fsum1=fsum2=dsum1=dsum2=0; | |
for (i=1;i<=n;i++) { | |
fsum1+=1.0f/(i*i); | |
fsum2+=1.0f/((n-i+1)*(n-i+1)); | |
dsum1+=1.0/(i*i); | |
dsum2+=1.0/((n-i+1)*(n-i+1)); | |
} | |
printf ("%5d%11f%11f%11lf%11lf%15e%15e\n",n,fsum1,fsum2,dsum1,dsum2,dsum1-fsum1,dsum2-fsum2); | |
} | |
return 0; | |
} | |
/* | |
Result: | |
N Float S1 Float S2 Double S1 Double S2 Error S1 Error S2 | |
1000 1.643935 1.643934 1.643935 1.643935 -2.792428e-007 7.838505e-008 | |
2000 1.644432 1.644434 1.644434 1.644434 2.123956e-006 -2.181091e-008 | |
3000 1.644595 1.644601 1.644601 1.644601 6.119722e-006 4.004847e-008 | |
4000 1.644714 1.644684 1.644684 1.644684 -2.978054e-005 2.178642e-008 | |
5000 1.644725 1.644734 1.644734 1.644734 8.764124e-006 -5.736392e-008 | |
6000 1.644725 1.644767 1.644767 1.644767 4.209135e-005 1.046708e-008 | |
7000 1.644725 1.644791 1.644791 1.644791 6.589719e-005 -2.555155e-008 | |
8000 1.644725 1.644809 1.644809 1.644809 8.375194e-005 6.701575e-008 | |
9000 1.644725 1.644823 1.644823 1.644823 9.763919e-005 6.778195e-009 | |
10000 1.644725 1.644834 1.644834 1.644834 1.087491e-004 3.025260e-008 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment