PDA

View Full Version : Nick Hodges takes a crack at Calculating Pi



WILL
21-08-2011, 04:37 AM
Nick Hodges was a Borland and Embarcadero developer up until less than a year ago. Not sure why he left the company, but he seems to be just as hooked into the internet as he ever was. Here is a post from his blog where he posts his code that will calculate a rough value of Pi.

http://www.nickhodges.com/post/Calculating-Pi.aspx

User137
21-08-2011, 05:33 AM
I didn't know boolean could be used with case... But for the compiler it propably produces 2 if clauses which takes away more cpu cycles.

From the comments, this does seem more optimized approach:

function CalculatePi(aIterations: Cardinal): Extended;
var
Counter: Cardinal;
Denominator, Sign: integer;
begin
Counter := 0;
Denominator := 3;
Sign := -1;
Result := 1.0;

repeat
Result := Result + (Sign/Denominator);
Inc(Counter);
Inc(Denominator, 2); // if you like Inc, go all the way
Sign := -Sign;
until Counter >= aIterations ;

Result := Result * 4.0;
end;