Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #define pi 3.14159265358979323846264338
- double sympson(double top, double middle, double bottom, double cell){
- return (top + 4.0 * middle + bottom) * cell/6.0;
- }
- int main(void){
- int n, i;
- double sum;
- for(n = 2; n <= 4096;n *= 2){
- sum=0.0;
- for(i=0;i<n;i++){
- sum += sympson(1/( 1.0 + ((double)i/n) * ((double)i/n) ),
- 1/( 1.0 + ((double)(i*2.0+1.0)/(n * 2.0)) * ((double)(i*2.0+1.0)/(n * 2.0)) ),
- 1/(1.0 + ((double)(i+1.0)/n) * ((double)(i+1.0)/n)),
- 1.0/n);
- }
- printf("%d %18.16lf %18.16lf\n", n, 4.0 * sum, fabs(4.0 * sum - pi));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment