Skip to content

Small change to monte_carlo.c #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 17, 2018

Conversation

Gathros
Copy link
Contributor

@Gathros Gathros commented Oct 12, 2018

No description provided.

@Gathros Gathros added Implementation Edit This provides an edit to an algorithm implementation. (Code and maybe md files are edited.) Hacktoberfest The label for all Hacktoberfest related things! labels Oct 12, 2018
Copy link
Contributor

@Gorzoid Gorzoid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good, removing the radius seems fair given that its not at all necessary to calculate pi.


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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a mistake here when last fixxing. %\n needs to be %%\n otherwise we get a format warning. Also I am reconsidering limiting the decimal places of the percentage. What do you think

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're at it, let's convert everything inside monte_carlo from int to unsigned.

}

double monte_carlo(int samples) {
double radius = 1.0;
double monte_carlo(unsigned int samples) {
int count = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be unsigned int.

}

double monte_carlo(int samples) {
double radius = 1.0;
double monte_carlo(unsigned int samples) {
int count = 0;

for (int i = 0; i < samples; ++i) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be unsigned int.

@leios
Copy link
Member

leios commented Oct 16, 2018

It looks like this code is fine? Is there anything left to be done here @berquist ?

@berquist
Copy link
Member

I am happy.

@berquist berquist merged commit 9f8b4c8 into algorithm-archivists:master Oct 17, 2018
@Gathros Gathros deleted the monte_change branch October 20, 2018 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hacktoberfest The label for all Hacktoberfest related things! Implementation Edit This provides an edit to an algorithm implementation. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants