-
-
Notifications
You must be signed in to change notification settings - Fork 360
Add Monte Carlo Integration in C# #309
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
Add Monte Carlo Integration in C# #309
Conversation
System.Console.WriteLine($"The estimate of pi is: {output.PiEstimate}"); | ||
System.Console.WriteLine($"The error is: {output.Error}"); | ||
|
||
System.Console.WriteLine("How many samples should be used?"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I leave this custom user input in?
|
||
public Circle(double radius) => this.Radius = Math.Abs(radius); | ||
|
||
public bool IsInMe(Point point) => point.X * point.X + point.Y * point.Y < Radius * Radius; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used point.X * point.X
now instead of using Math.Pow
. It doesn't really matter here, what do you find cleaner?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just leave Math.Pow ... it look then more compact and less chance of missreading
} | ||
|
||
var piEstimate = 4.0 * count / samples; | ||
var error = Math.Abs(Math.PI - piEstimate); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make the error a percentage (and add % when you print out the error) to remove ambiguity
static void Main(string[] args) | ||
{ | ||
var monteCarlo = new MonteCarlo(); | ||
System.Console.WriteLine("Running with 10.000.000 samples."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe it should be 10,000,000
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, just thought german there :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use 10 000 000
then or word notation 10 million
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think 10,000,000 is fine since the example is in english.
System.Console.WriteLine($"The estimate of pi is: {piEstimate.Estimate}"); | ||
System.Console.WriteLine($"The error is: {piEstimate.Error}"); | ||
|
||
System.Console.WriteLine("How many samples should be used?"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally would not include this input part's to code examples. I would keep them minimal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally like to give the user a choice here, but I understand that it makes things quite a bit more messy.
…adable. Use percent error. Remove user input.
} | ||
|
||
var piEstimate = 4.0 * count / samples; | ||
var percentError = (piEstimate - Math.PI) / Math.PI * 100; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last thing: it feels strange to me to use Math.PIinside of a function that estimates pi. I've usually recommended to other people to calculate the error in main.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I see what you mean. I'm gonna change that.
Whoa, +4 -18 for moving a single line. OOP really is verbose :) |
@jiegillet I didn't need that output class anymore :D |
Since this PR is dead, I wrote it myself.
Math.Pow
instead ofpoint.X * point.X
.10,000,000
instead of10.000.000
for the console output.