home *** CD-ROM | disk | FTP | other *** search
- Program CalcPi;
-
- uses crt, dos, vlncls;
-
- var
- i, j, terms : integer;
- CountMax,StdVLNSize : integer;
- a, b, c, d, r, s : pVryLrgNo;
-
-
- begin
- { wksize is initialized to 100 words of storage }
- { but we make it larger before any memory is allocated }
- Setwksize(1000); { 1000 words = 5000 decimal digits }
- OpenTempRegs;
- { standard subs need temp storage }
- { their max count is wksize }
- { their needed storage is wksize*2+6 }
-
- CountMax := Getwksize; { set count to anyvalue <= wksize }
- StdVLNSize := CountMax*2+6; {these variables need this much storage }
-
- getmem(a, StdVLNSize);
- getmem(b, StdVLNSize);
- getmem(c, StdVLNSize);
- getmem(d, StdVLNSize);
- getmem(r, StdVLNSize);
- getmem(s, StdVLNSize);
-
- a^.init(0,CountMax,1, nil);
- b^.init(0,CountMax,1, nil);
- c^.init(0,CountMax,1, nil);
- d^.init(0,CountMax,1, nil);
- r^.init(0,CountMax,1, nil);
- s^.init(0,CountMax,1, nil);
-
- writeln('--------------------------------');
-
- { quick calculation of pi ^4 / 96 }
- { 1 + 1/3^4 + 1/5^4 + + + }
- {}
- repeat
- writeln;
- write('How many terms - ');
- readln(terms);
- if terms=0 then break;
-
- c^.Setsmall(10000);
- c^.NthPower(9*3); { 27 decimal places }
- { c^.writeDecimal(0); { c is constant scaled 1.0 }
-
-
- a^.setsmall(1); { first term gives 1 }
- a^.MulBy(c); { a is accumulator }
-
- for i := 1 to terms-1 do
- begin
- j := i*2+1;
- d^.setsmall(j); { an odd integer }
- d^.NthPower(4); { 4th power of the integer }
- b^.copy(c); { 1.0 scaled by 10^36 }
- b^.divby(d,r); { next term }
- s^.copy(a); { save next to last answer }
- a^.addby(b);
- write(';');
- end;
-
- a^.MulN(96);
- a^.NthRoot(4);
- write('PI = '); a^.writeDecimal(0) ; writeln;
-
- s^.MulN(96);
- s^.NthRoot(4);
- s^.subby(a);
- write('change last iteration - '); s^.writeDecimal(0) ; writeln;
- until terms<=0;
-
- writeln('--------------------------------');
- writeln('Done!');
-
- freemem(a, StdVLNSize);
- freemem(b, StdVLNSize);
- freemem(c, StdVLNSize);
- freemem(d, StdVLNSize);
- freemem(r, StdVLNSize);
- freemem(s, StdVLNSize);
-
- CloseTempRegs;
- end.