home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / math / verylarg / pi_4_96.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-10-22  |  2.5 KB  |  89 lines

  1. Program CalcPi;
  2.  
  3. uses    crt, dos, vlncls;
  4.  
  5. var
  6.      i, j, terms  : integer;
  7.      CountMax,StdVLNSize : integer;
  8.      a, b, c, d, r, s       : pVryLrgNo;
  9.  
  10.  
  11. begin
  12.     { wksize is initialized to 100 words of storage }
  13.     { but we make it larger before any memory is allocated }
  14.     Setwksize(1000);      { 1000 words = 5000 decimal digits }
  15.     OpenTempRegs;
  16.                             { standard subs need temp storage }
  17.                             { their max count is wksize      }
  18.                             { their needed storage is wksize*2+6 }
  19.  
  20.     CountMax := Getwksize;      { set count to anyvalue <= wksize }
  21.     StdVLNSize := CountMax*2+6; {these variables need this much storage }
  22.  
  23.     getmem(a, StdVLNSize);
  24.     getmem(b, StdVLNSize);
  25.     getmem(c, StdVLNSize);
  26.     getmem(d, StdVLNSize);
  27.     getmem(r, StdVLNSize);
  28.     getmem(s, StdVLNSize);
  29.  
  30.     a^.init(0,CountMax,1, nil);
  31.     b^.init(0,CountMax,1, nil);
  32.     c^.init(0,CountMax,1, nil);
  33.     d^.init(0,CountMax,1, nil);
  34.     r^.init(0,CountMax,1, nil);
  35.     s^.init(0,CountMax,1, nil);
  36.  
  37.     writeln('--------------------------------');
  38.  
  39.     { quick calculation of pi ^4 / 96 }
  40.     { 1 + 1/3^4 + 1/5^4 + + + }
  41.     {}
  42.     repeat
  43.        writeln;
  44.        write('How many terms - ');
  45.        readln(terms);
  46.        if terms=0 then break;
  47.  
  48.        c^.Setsmall(10000);
  49.        c^.NthPower(9*3);  { 27 decimal places }
  50. {       c^.writeDecimal(0);    { c is constant scaled 1.0 }
  51.  
  52.  
  53.        a^.setsmall(1);        { first term gives 1 }
  54.        a^.MulBy(c);           { a is accumulator }
  55.  
  56.        for i := 1 to terms-1 do
  57.           begin
  58.             j := i*2+1;
  59.             d^.setsmall(j);  { an odd integer }
  60.             d^.NthPower(4);  { 4th power of the integer }
  61.             b^.copy(c);      { 1.0 scaled by 10^36 }
  62.             b^.divby(d,r);     { next term }
  63.             s^.copy(a);        { save next to last answer }
  64.             a^.addby(b);
  65.             write(';');
  66.           end;
  67.  
  68.        a^.MulN(96);
  69.        a^.NthRoot(4);
  70.        write('PI = '); a^.writeDecimal(0) ; writeln;
  71.  
  72.        s^.MulN(96);
  73.        s^.NthRoot(4);
  74.        s^.subby(a);
  75.        write('change last iteration - '); s^.writeDecimal(0) ; writeln;
  76.     until terms<=0;
  77.  
  78.     writeln('--------------------------------');
  79.     writeln('Done!');
  80.  
  81.     freemem(a, StdVLNSize);
  82.     freemem(b, StdVLNSize);
  83.     freemem(c, StdVLNSize);
  84.     freemem(d, StdVLNSize);
  85.     freemem(r, StdVLNSize);
  86.     freemem(s, StdVLNSize);
  87.  
  88.     CloseTempRegs;
  89.     end.