in reply to probabilistic pi algorithm

You can simplify this quite a bit if you use a radius of 1, rather than a diameter of 1000. When R = 1, the test becomes $x * $x  + $y * $y <= 1.

(Very) Old Habits Die Hard. Hence my use of $x*$x rather than $x**2. I doubt there's much difference on modern processors.

Update: s/\^/**/. Thanks to kyle for pointing it out

Replies are listed 'Best First'.
Re^2: probabilistic pi algorithm
by bart (Canon) on Feb 16, 2008 at 09:30 UTC
    Hence my use of $x*$x rather than $x**2. I doubt there's much difference on modern processors.
    Oh yes it does make a difference. $x*$x does a multiplication of two numbers, while $x**2 goes the long route of exp(log($x)*2), as ** is a very generic power raising operator. Not only will it take longer (maybe negligible these days, like you say), but most of all: its result will be less precise: both log and exp have quite large margins of error.
Re^2: probabilistic pi algorithm
by spx2 (Deacon) on Feb 16, 2008 at 09:34 UTC
    hi,

    Your ideea is very nice,I thought about it
    so if we take $d=2 then the radius which in this case is $d/2=1
    so now we can have the formula ($x-1)**2 + ($y-1)**2 <= 1
    exactly as you write in your comment.
    About the difference between $x*$x and $x**2,what optimisations do you think could be
    made ?
      Subroutine calls in Perl are expensive, especially when there's a few million of them, so inline as much as you can.

      Benchmarking 3 variants reveals...

      Rate original multiply powers original 0.405/s -- -72% -76% multiply 1.45/s 257% -- -14% powers 1.69/s 317% 17% --

      Further optimization is of little value in terms of convergence. The problem is in the random-walk nature of the convergence. While the quotient is guaranteed to eventually converge, over a given interval the quotient can diverge. While convergence is guaranteed the time over which it converges is not guaranteed.


      s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}