CATEGORY:
Knuth: Blunder (Thinking the right thing but doing it wrong)
REFERENCE:
https://www.tug.org/TUGboat/tb10-4/tb26knut.pdf
EXPLANATION:
As a matter of fact, every point that is within the circle (Ac) is also
within the square (As). Hoever, the code, as it stands, increments
'in_square_count' only if a point is located outside the circle.
CONSEQUENCES:
'in_square_count' is incremented too few times and consequently, the
computed value of PI is too big (around 14.6), refer to REMARKS section
below.
BUGFIX:
Since every random point must be within the square, update variable
'in_square_count' with every loop iteration:
if (is_in_circle(x, y)) {
++in_circle_count;
}
++in_square_count;
REMARKS:
What value does 'monte_carlo_pi' really return?
Because 'in_square_count' is updated (wrongly) only if a point is located
outside the circle (within the area between the circle and the square),
instead of 4 Ac/As, 4 Ac/(As-Ac) is computed:
4 Ac/(As - Ac) = 4 (r^2 * PI)/((2 * r)^2 - r^2 * PI)
= 4 PI / (4 - PI)
= 14.639...