Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // metodo de monte carlo para estimar o valor de pi
- // apple xcode
- // paulogp
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define TOLMAX 1.0e-8
- #define NPONTOS 5000
- int main (int argc, const char * argv[])
- {
- /* metodo de monte carlo para estimar o valor de Pi */
- int the_total_pontos = 0;
- int the_pontos_dentro = 0;
- int the_p;
- float the_x;
- float the_y;
- float the_area_ant = 0.0;
- float the_area_nov = 0.0;
- float the_tol = 1.0;
- while (the_tol > TOLMAX)
- {
- the_p = 1;
- while (the_p <= NPONTOS)
- {
- the_x = (float) rand() / RAND_MAX;
- the_y = (float) rand() / RAND_MAX;
- if ((the_x * the_x + the_y * the_y) < 1)
- {
- the_pontos_dentro++;
- }
- the_p++;
- }
- the_total_pontos = the_total_pontos + NPONTOS;
- the_area_nov = (float) the_pontos_dentro / the_total_pontos;
- printf("area para %d pontos: %f\n", the_total_pontos, the_area_nov);
- the_tol = fabs(the_area_nov - the_area_ant);
- the_area_ant = the_area_nov;
- }
- printf("estimativa de pi = %6.3f\n", 4 * the_area_nov);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement