From 1f81d92c879f46b3ca00e54796426c0737d008f6 Mon Sep 17 00:00:00 2001 From: Gathros <6323830+Gathros@users.noreply.github.com> Date: Fri, 12 Oct 2018 12:07:24 +0100 Subject: [PATCH 1/4] Small change to monte_carlo.c --- .../code/c/monte_carlo.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/contents/monte_carlo_integration/code/c/monte_carlo.c b/contents/monte_carlo_integration/code/c/monte_carlo.c index 961c2f546..c5929a9e3 100644 --- a/contents/monte_carlo_integration/code/c/monte_carlo.c +++ b/contents/monte_carlo_integration/code/c/monte_carlo.c @@ -4,19 +4,18 @@ #include #include -bool in_circle(double x, double y, double radius) { - return x * x + y * y < radius * radius; +bool in_circle(double x, double y) { + return x * x + y * y < 1; } double monte_carlo(int samples) { - double radius = 1.0; int count = 0; for (int i = 0; i < samples; ++i) { - double x = (double)rand() * radius / RAND_MAX; - double y = (double)rand() * radius / RAND_MAX; + double x = (double)rand() / RAND_MAX; + double y = (double)rand() / RAND_MAX; - if (in_circle(x, y, radius)) { + if (in_circle(x, y)) { count += 1; } } @@ -28,9 +27,9 @@ int main() { srand(time(NULL)); double estimate = monte_carlo(1000000); - - printf("The estimate of pi is %f\n", estimate); + + printf("The estimate of pi is %g\n", estimate); printf("Percentage error: %0.2f%\n", 100 * fabs(M_PI - estimate) / M_PI); - + return 0; } From 71054da910ebfa6186ffca8d02ab7f3d610ecd73 Mon Sep 17 00:00:00 2001 From: Gathros <6323830+Gathros@users.noreply.github.com> Date: Fri, 12 Oct 2018 22:40:10 +0100 Subject: [PATCH 2/4] fixing formating --- contents/monte_carlo_integration/code/c/monte_carlo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/monte_carlo_integration/code/c/monte_carlo.c b/contents/monte_carlo_integration/code/c/monte_carlo.c index c5929a9e3..d84d2d691 100644 --- a/contents/monte_carlo_integration/code/c/monte_carlo.c +++ b/contents/monte_carlo_integration/code/c/monte_carlo.c @@ -29,7 +29,7 @@ int main() { double estimate = monte_carlo(1000000); printf("The estimate of pi is %g\n", estimate); - printf("Percentage error: %0.2f%\n", 100 * fabs(M_PI - estimate) / M_PI); + printf("Percentage error: %0.2f%%\n", 100 * fabs(M_PI - estimate) / M_PI); return 0; } From 8a052512d7a42e2e8e1edf45e87e2abdcc5c87f8 Mon Sep 17 00:00:00 2001 From: Gathros <6323830+Gathros@users.noreply.github.com> Date: Sat, 13 Oct 2018 08:51:48 +0100 Subject: [PATCH 3/4] small change --- contents/monte_carlo_integration/code/c/monte_carlo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/monte_carlo_integration/code/c/monte_carlo.c b/contents/monte_carlo_integration/code/c/monte_carlo.c index d84d2d691..94071ac75 100644 --- a/contents/monte_carlo_integration/code/c/monte_carlo.c +++ b/contents/monte_carlo_integration/code/c/monte_carlo.c @@ -8,7 +8,7 @@ bool in_circle(double x, double y) { return x * x + y * y < 1; } -double monte_carlo(int samples) { +double monte_carlo(unsigned int samples) { int count = 0; for (int i = 0; i < samples; ++i) { From 76d86254d37f7cc6696dd7f7a2e6fa63bc0504c9 Mon Sep 17 00:00:00 2001 From: Gathros <6323830+Gathros@users.noreply.github.com> Date: Sun, 14 Oct 2018 09:05:10 +0100 Subject: [PATCH 4/4] changing to uint --- contents/monte_carlo_integration/code/c/monte_carlo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contents/monte_carlo_integration/code/c/monte_carlo.c b/contents/monte_carlo_integration/code/c/monte_carlo.c index 94071ac75..9920ff55c 100644 --- a/contents/monte_carlo_integration/code/c/monte_carlo.c +++ b/contents/monte_carlo_integration/code/c/monte_carlo.c @@ -9,9 +9,9 @@ bool in_circle(double x, double y) { } double monte_carlo(unsigned int samples) { - int count = 0; + unsigned int count = 0; - for (int i = 0; i < samples; ++i) { + for (unsigned int i = 0; i < samples; ++i) { double x = (double)rand() / RAND_MAX; double y = (double)rand() / RAND_MAX;