Results 1 to 2 of 2

Thread: Nick Hodges takes a crack at Calculating Pi

  1. #1
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Cool Nick Hodges takes a crack at Calculating Pi

    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
    Jason McMillen
    Pascal Game Development
    Co-Founder





  2. #2
    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:
    Code:
    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;

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •