int main(void) { double abs_error; int n = 100000; double pi; printf("Leibniz series \n"); do{ pi = pi_leibniz(n); abs_error = fabs(pi - M_PI); printf("%8d %20.15f %20.15f\n", n, pi, abs_error); n += 100000; }while(abs_error > .000001); printf("BBP\n"); n = 1; do{ pi = pi_bbp(n); abs_error = fabs(pi - M_PI); printf("%8d %20.15f %20.15f\n", n, pi, abs_error); n += 1; }while(abs_error > .000001); printf("\n"); clock_t start, stop; int n_one = 1000000; int n_two = 4; int leib_runs = 100; start = clock(); for(int i = 0; i < leib_runs; i++){ pi = pi_leibniz(n_one); } stop = clock(); double t1 = 1000*((double)(stop-start))/(CLOCKS_PER_SEC*leib_runs); printf("t1 %.6f \n", t1); int bbp_runs = 2000000; start = clock(); for(int i = 0; i < bbp_runs; i++){ pi = pi_bbp(n_two); } stop = clock(); double t2 = 1000*((double)(stop-start))/(1.0*CLOCKS_PER_SEC*bbp_runs); printf("t2 %.6f \n", t2); printf("%21.0f \n", t1/t2); return 0; }
int main(void) { // j and n are just used as iteration variables n int i = 1; double pi; double abserr; double tol = pow ( 10 , -6 ); do { //doubles the amount of terms in the pi_leibniz series until error is less than 10^-6 pi = pi_leibniz (i); abserr = fabs (pi - M_PI); printf ("%8d %20.15f %20.15f\n", i, pi, abserr); i *= 2; } while ( abserr > tol ); i = 1; do { //doubles the amount of terms in the pi_bbp series until error is less than 10^-6 pi = pi_bbp (i); abserr = fabs (pi - M_PI); printf ("%8d %20.15f %20.15f\n", i, pi, abserr); i *= 2; } while ( abserr > tol ); // end of code figuring out number of terms needed for 10^-6 accuracy int count = 10; double time; double time1; double tmin = 1; double tmax = 2; printf("pi_leibniz\n"); do { timer_start (); for (int k = 0; k <= count; k++) { pi_leibniz (1048576); } time = timer_stop (); time1 = time / count; printf (" %10.2f usec %10.6f sec %10d\n", time1 * 1.e6, time, count); /* * adjust count such that cpu time is between * tmin and tmax */ count = adjust_rep_count (count, time, tmin, tmax); } while ((time > tmax) || (time < tmin)); printf("pi_pbb\n"); do { timer_start (); for (int k = 0; k < count; k++) { pi_bbp (4); } time = timer_stop (); time1 = time / count; printf (" %10.2f usec %10.6f sec %10d\n", time1 * 1.e6, time, count); /* * adjust count such that cpu time is between * tmin and tmax */ count = adjust_rep_count (count, time, tmin, tmax); } while ((time > tmax) || (time < tmin)); return 0; }