Révision 30
Pi/C/Simple/Pi.c (revision 30) | ||
---|---|---|
6 | 6 |
#include <math.h> |
7 | 7 |
#include <stdio.h> |
8 | 8 |
#include <stdlib.h> |
9 |
#include <limits.h> |
|
9 | 10 |
|
10 | 11 |
// Marsaglia RNG very simple implementation |
11 | 12 |
#define znew ((z=36969*(z&65535)+(z>>16))<<16) |
... | ... | |
22 | 23 |
#define ITERATIONS 1000000000 |
23 | 24 |
|
24 | 25 |
#ifdef LONG |
25 |
#define LENGTH unsigned long
|
|
26 |
#define LENGTH long long
|
|
26 | 27 |
#else |
27 |
#define LENGTH unsigned int
|
|
28 |
#define LENGTH int |
|
28 | 29 |
#endif |
29 | 30 |
|
30 | 31 |
LENGTH MainLoopGlobal(LENGTH iterations,unsigned int seed_w,unsigned int seed_z) |
... | ... | |
32 | 33 |
unsigned int z=seed_z; |
33 | 34 |
unsigned int w=seed_w; |
34 | 35 |
|
35 |
unsigned long total=0;
|
|
36 |
LENGTH total=0;
|
|
36 | 37 |
|
37 | 38 |
for (LENGTH i=0;i<iterations;i++) { |
38 | 39 |
|
... | ... | |
66 | 67 |
LENGTH iterations=ITERATIONS; |
67 | 68 |
|
68 | 69 |
if (argc > 1) { |
69 |
iterations=(LENGTH)atol(argv[1]); |
|
70 |
iterations=(LENGTH)atoll(argv[1]);
|
|
70 | 71 |
} |
71 | 72 |
else { |
72 | 73 |
printf("\n\tPi : Estimate Pi with Monte Carlo exploration\n\n\t\t#1 : number of iterations (default 1 billion)\n\n"); |
73 | 74 |
} |
74 | 75 |
|
76 |
printf ("\n\tInformation about architecture:\n\n"); |
|
77 |
|
|
78 |
printf ("\tSizeof int = %lld bytes.\n", (long long)sizeof(int)); |
|
79 |
printf ("\tSizeof long = %lld bytes.\n", (long long)sizeof(long)); |
|
80 |
printf ("\tSizeof long long = %lld bytes.\n\n", (long long)sizeof(long long)); |
|
81 |
|
|
82 |
printf ("\tMax int = %u\n", INT_MAX); |
|
83 |
printf ("\tMax long = %ld\n", LONG_MAX); |
|
84 |
printf ("\tMax long long = %lld\n\n", LLONG_MAX); |
|
85 |
|
|
75 | 86 |
float pi=(float)MainLoopGlobal(iterations,seed_w,seed_z)/(float)iterations*4; |
76 | 87 |
|
77 |
printf("\tPi=%.40f\n\twith error %.40f\n\twith %lu iterations\n\n",pi,
|
|
78 |
fabs(pi-4*atan(1))/pi,(unsigned long)iterations);
|
|
88 |
printf("\tPi=%.40f\n\twith error %.40f\n\twith %lld iterations\n\n",pi,
|
|
89 |
fabs(pi-4*atan(1))/pi,(long long)iterations);
|
|
79 | 90 |
|
80 | 91 |
} |
Formats disponibles : Unified diff