/* * C program to compute the value of pi using Monte Carlo method. * * command-line arguments: number_of_samples, seed */ #include #include #include /* has fabs() */ /* copied from not-strictly-standard part of math.h */ #define M_PI 3.14159265358979323846 #include "timer.h" /* has get_time() */ /* main program */ int main(int argc, char* argv[]) { char* usage_fmt = "usage: %s number_of_samples seed\n"; char* end_ptr_for_strtol; /* process command-line arguments */ if (argc != 3) { fprintf(stderr, usage_fmt, argv[0]); exit(EXIT_FAILURE); } long num_samples = strtol(argv[1], &end_ptr_for_strtol, 10); if (*end_ptr_for_strtol != '\0') { fprintf(stderr, usage_fmt, argv[0]); exit(EXIT_FAILURE); } long seed = strtol(argv[2], &end_ptr_for_strtol, 10); if (*end_ptr_for_strtol != '\0') { fprintf(stderr, usage_fmt, argv[0]); exit(EXIT_FAILURE); } /* record start time */ double start_time = get_time(); /* do calculation */ srand((unsigned int) seed); long count = 0; double x, y; for (long i = 0; i < num_samples; ++i) { x = (double) rand() / (double) (RAND_MAX); y = (double) rand() / (double) (RAND_MAX); if ((x*x + y*y) <= 1.0) ++count; } double pi = 4.0 * (double) count / (double) num_samples; /* record end time and print results */ double end_time = get_time(); printf("sequential C program results:\n"); printf("number of samples = %ld, seed = %ld\n", num_samples, seed); printf("estimated pi = %12.10f\n", pi); printf("difference between estimated pi and math.h M_PI = %12.10f\n", fabs(pi - M_PI)); printf("time to compute = %g seconds\n", end_time - start_time); /* clean up and return */ return EXIT_SUCCESS; }