| [e48510a] | 1 | /***************************************************************************
|
|---|
| 2 | * FILE: ser_pi_calc.c
|
|---|
| 3 | * DESCRIPTION:
|
|---|
| 4 | * Serial pi Calculation - C Version
|
|---|
| 5 | * This program calculates pi using a "dartboard" algorithm. See
|
|---|
| 6 | * Fox et al.(1988) Solving Problems on Concurrent Processors, vol.1
|
|---|
| 7 | * page 207.
|
|---|
| 8 | * AUTHOR: unknown
|
|---|
| 9 | * REVISED: 02/23/12 Blaise Barney
|
|---|
| 10 | ***************************************************************************/
|
|---|
| 11 | #include <stdio.h>
|
|---|
| 12 | #include <stdlib.h>
|
|---|
| 13 |
|
|---|
| 14 | void srandom (unsigned seed);
|
|---|
| 15 | double dboard (int darts);
|
|---|
| 16 |
|
|---|
| 17 | #define DARTS 10000 /* number of throws at dartboard */
|
|---|
| 18 | #define ROUNDS 100 /* number of times "darts" is iterated */
|
|---|
| 19 |
|
|---|
| 20 | int main(int argc, char *argv[])
|
|---|
| 21 | {
|
|---|
| 22 | double pi; /* average of pi after "darts" is thrown */
|
|---|
| 23 | double avepi; /* average pi value for all iterations */
|
|---|
| 24 | int i, n;
|
|---|
| 25 |
|
|---|
| 26 | printf("Starting serial version of pi calculation using dartboard algorithm...\n");
|
|---|
| 27 | srandom (5); /* seed the random number generator */
|
|---|
| 28 | avepi = 0;
|
|---|
| 29 | for (i = 0; i < ROUNDS; i++) {
|
|---|
| 30 | /* Perform pi calculation on serial processor */
|
|---|
| 31 | pi = dboard(DARTS);
|
|---|
| 32 | avepi = ((avepi * i) + pi)/(i + 1);
|
|---|
| 33 | printf(" After %3d throws, average value of pi = %10.8f\n",
|
|---|
| 34 | (DARTS * (i + 1)),avepi);
|
|---|
| 35 | }
|
|---|
| 36 | printf("\nReal value of PI: 3.1415926535897 \n");
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | /*****************************************************************************
|
|---|
| 41 | * dboard
|
|---|
| 42 | *****************************************************************************/
|
|---|
| 43 | #define sqr(x) ((x)*(x))
|
|---|
| 44 | long random(void);
|
|---|
| 45 |
|
|---|
| 46 | double dboard(int darts)
|
|---|
| 47 | {
|
|---|
| 48 | double x_coord, /* x coordinate, between -1 and 1 */
|
|---|
| 49 | y_coord, /* y coordinate, between -1 and 1 */
|
|---|
| 50 | pi, /* pi */
|
|---|
| 51 | r; /* random number scaled between 0 and 1 */
|
|---|
| 52 | int score, /* number of darts that hit circle */
|
|---|
| 53 | n;
|
|---|
| 54 | unsigned int cconst; /* must be 4-bytes in size */
|
|---|
| 55 | /*************************************************************************
|
|---|
| 56 | * The cconst variable must be 4 bytes. We check this and bail if it is
|
|---|
| 57 | * not the right size
|
|---|
| 58 | ************************************************************************/
|
|---|
| 59 | if (sizeof(cconst) != 4) {
|
|---|
| 60 | printf("Wrong data size for cconst variable in dboard routine!\n");
|
|---|
| 61 | printf("See comments in source file. Quitting.\n");
|
|---|
| 62 | exit(1);
|
|---|
| 63 | }
|
|---|
| 64 | /* 2 bit shifted to MAX_RAND later used to scale random number between 0 and 1 */
|
|---|
| 65 | cconst = 2 << (31 - 1);
|
|---|
| 66 | score = 0;
|
|---|
| 67 |
|
|---|
| 68 | /***********************************************************************
|
|---|
| 69 | * Throw darts at board. Done by generating random numbers
|
|---|
| 70 | * between 0 and 1 and converting them to values for x and y
|
|---|
| 71 | * coordinates and then testing to see if they "land" in
|
|---|
| 72 | * the circle." If so, score is incremented. After throwing the
|
|---|
| 73 | * specified number of darts, pi is calculated. The computed value
|
|---|
| 74 | * of pi is returned as the value of this function, dboard.
|
|---|
| 75 | ************************************************************************/
|
|---|
| 76 |
|
|---|
| 77 | for (n = 1; n <= darts; n++) {
|
|---|
| 78 | /* generate random numbers for x and y coordinates */
|
|---|
| 79 | r = (double)random()/cconst;
|
|---|
| 80 | x_coord = (2.0 * r) - 1.0;
|
|---|
| 81 | r = (double)random()/cconst;
|
|---|
| 82 | y_coord = (2.0 * r) - 1.0;
|
|---|
| 83 |
|
|---|
| 84 | /* if dart lands in circle, increment score */
|
|---|
| 85 | if ((sqr(x_coord) + sqr(y_coord)) <= 1.0)
|
|---|
| 86 | score++;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /* calculate pi */
|
|---|
| 90 | pi = 4.0 * (double)score/(double)darts;
|
|---|
| 91 | return(pi);
|
|---|
| 92 | }
|
|---|