[erlang-questions] Calculate PI in Erlang faster than in Matlab

Jilani Khaldi jilani@REDACTED
Thu Apr 3 09:22:21 CEST 2008


> Are you Free Pascal version uses N=1000000 steps? Maybe you have some
> epsilon-optimization, like Jeff suggested before? Can you send me your FP
> code?
Sure.

program pi;
{$mode delphi} {$H+}

uses
   sysutils;

const
   N = 1000000;
   DX = 1/N;
   kPI = 3.14159;

var
   i: integer;
   x, p, dp: double;
   HH, MM, SS1, MS1, SS2, MS2: Word;
   st: string;
begin
   p := 0;
   x := 0;
   DeCodeTime(Time,HH,MM,SS1,MS1);
   for i := 1 to N do
   begin
     dp := 4*DX/(1+x*x);
     p := p + dp;
     x := x + DX;
   end;
   DeCodeTime (Time,HH,MM,SS2,MS2);
   SS2 := SS2 - SS1;
   MS2 := MS2 - MS1;
   st := Format('PI = %1.8f; Seconds = %d; MSeconds = %d', [p, SS2, MS2]);
   writeln(st);

   // Now looping till we to reach 3.14159
   p := 0;
   x := 0;
   i := 0; // Just to know how many loops
   DeCodeTime(Time,HH,MM,SS1,MS1);
   while p < kPI do
   begin
     dp := 4*DX/(1+x*x);
     p := p + dp;
     x := x + DX;
     inc(i);
   end;
   DeCodeTime (Time,HH,MM,SS2,MS2);
   SS2 := SS2 - SS1;
   MS2 := MS2 - MS1;
   st := Format('Loops Nr = %d; PI = %1.8f; Seconds = %d; MSeconds = 
%d', [i, p, SS2, MS2]);
   writeln(st);
end.

-- 
***
T E M A
http://www.dotpas.org



More information about the erlang-questions mailing list