Skip to content

Commit 2b2b5c3

Browse files
authored
Small improvements and absolute percent error for the Julia Monte Carlo (#325)
* Print pi estimate. * Fix percent error calculation. * Make the percent error always be a positive value, by using the absolute value of a critical calculation step. Also space out the calculation of the error a bit.
1 parent bcb9131 commit 2b2b5c3

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

contents/monte_carlo_integration/code/julia/monte_carlo.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ function monte_carlo(n::Int64)
2424
# are only using the upper quadrant and the unit circle, so we can use
2525
# 4*pi_count/n instead
2626
pi_estimate = 4*pi_count/n
27-
println("Percent error is: ", signif(100*(pi - pi_estimate)/pi, 3), " %")
27+
println("The pi estimate is: ", pi_estimate)
28+
println("Percent error is: ", signif(100 * abs(pi_estimate - pi) / pi, 3), " %")
2829
end
2930

3031
monte_carlo(10000000)

0 commit comments

Comments
 (0)