-
-
Notifications
You must be signed in to change notification settings - Fork 360
Added Monte Carlo in Swift 4.1 #197
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
Added Monte Carlo in Swift 4.1 #197
Conversation
Added Monte Carlo in Swift 4.1
chapters/monte_carlo/monte_carlo.md
Outdated
@@ -113,6 +115,8 @@ Feel free to submit your version via pull request, and thanks for reading! | |||
{% sample lang="java" %} | |||
### Java | |||
[import, lang:"java"](code/java/MonteCarlo.java) | |||
### Swift |
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 missing {% sample lang="swift" %}
here
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.
Sorry about that just committed a change to fix it
|
||
func isInCircle(x: Double, y: Double, radius: Double) -> Bool { | ||
|
||
return (x*x) + (y*y) < Double(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.
Do you need to cast Double(radius*radius)
even though you have radius: Double
?
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 totally right I just committed a change to remove that redundancy
Removed unnecessary cast to Double
|
||
|
||
func main() { | ||
print("Pi estimate is: ", monteCarlo(n: 10000, radius: 50)) |
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.
It was a messy non-linear process, but we've moved away from using radius
as a free parameter, since it the result doesn't depend on the choice of radius
at all. Lose radius
from the monteCarlo
function and hardcode the radius to 1
inside.
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.
Sounds good I just updated my code accordingly 👍
} | ||
|
||
let piEstimate = Double(4 * piCount)/(Double(n)) | ||
print("Percent error is: \(100*(Double.pi - piEstimate)/Double.pi)%") |
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.
Move the error testing to 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.
Good suggestion placing it in main() makes more sense. I just made a commit which addresses this.
|
||
public static var random: Double { | ||
|
||
return Double(arc4random()) / 0xFFFFFFFF |
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.
What does that do? The hardcoded 0xFFFFFFFF
makes me uneasy :)
Maybe add a comment here?
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.
Sounds good added a comment which says what that line of code does
Sorry for the back and forth, I had written a review but had not realized it hadn't been shared yet! |
- Added comment - Removed radius as a free parameter for the monte carlo function - Moved error testing to main()
Thank you, looks great now :) |
Added Monte Carlo in Swift 4.1