home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-10-12 | 3.2 KB | 142 lines | [TEXT/MMCC] |
- {----------------------------------------------------------
-
- Pascal source code for a program which calculates the value of Pi.
-
- This program uses 80-bit number crunching to approximate values for pi.
- It also uses number tables to format the 80-bit number for output.
-
- This program was originally posted in C (author unknown), and I've
- translated it into Pascal. Error handling for formatting the number has
- been added to display error messages instead of a number if there are
- problems during formatting.
-
- This program demos simple uses of the following toolbox calls:
-
- GetIntlResourceTable
- BlockMove
- StringToFormatRec
-
- 10/7/95 - Bill Catambay, catambay@aol.com
- ----------------------------------------------------------}
-
- Program Pi;
-
- Uses
- fp, IntlResources, TextUtils, Fonts, Windows, Menus, Dialogs;
-
- Function Ipow(x: extended80; y: longint): extended80;
-
- { ipow returns an 80-bit floating point number raised to the power of a positive integer. }
-
- Var
- result: extended80;
- i: longint;
-
- begin
- result := 1;
- for i := 1 to y do
- result := result * x;
- Ipow := result;
- end;
-
- Function ApproxPi(iterations: longint): extended80;
-
- { ApproxPi returns an 80-bit approximation of π. }
-
- Var
- y,a,temp: extended80;
- i: longint;
-
- begin
- y := sqrt(2) - 1;
- a := 6 - 4 * sqrt(2);
- for i := 1 to iterations do
- begin
- temp := sqrt(sqrt(1 - ipow(y, 4)));
- y := (1 - temp) / (1 + temp);
- a := ipow(1 + y, 4) * a + ipow(-2.0, 2 * (i - 1) + 3) * y * (1 + y + y * y);
- end;
- ApproxPi := 1/a;
- end;
-
- Function FormatExtended(x: extended80; formatStr: str255): str255;
-
- Var
- itlHandle: Handle;
- offset,length: longint;
- partsTable: NumberParts;
- outString: NumFormatString;
- status: FormatStatus;
- displayStr: Str255;
-
- begin
- GetIntlResourceTable(smCurrentScript, smNumberPartsTable, itlHandle, offset, length);
- if itlHandle <> NIL then
- begin
- BlockMove(Ptr(ord4(itlHandle^) + offset), @partsTable, length);
- status := StringToFormatRec(formatStr, partsTable, outString);
- if status <> NoErr then
- begin
- FormatExtended := 'FORMAT ERROR';
- exit(FormatExtended);
- end;
- status := extendedToString(x, outString, partsTable, displayStr);
- if status = NoErr then
- FormatExtended := displayStr
- else
- FormatExtended := 'CONVERSION ERROR';
- end
- else
- FormatExtended := 'TABLE ERROR';
- end;
-
- Procedure DrawPi(pi: extended80);
-
- Const
- formatStr = '#.#################';
-
- begin
- Drawstring('π ≈ ');
- Drawstring(FormatExtended(pi,formatStr));
- end;
-
- Var
- window: WindowPtr;
- windowRect: Rect;
- pi: extended80;
-
- begin
- MaxApplZone;
- InitGraf(@qd.thePort);
- InitFonts;
- InitWindows;
- InitMenus;
- TEInit;
- InitDialogs(nil);
- SetRect(windowRect, 50, 70, 350, 220);
- window := NewWindow(nil, windowRect, '', true, dBoxProc, WindowPtr(-1), false, 0);
- if window <> NIL then
- begin
- SetPort(window);
- TextFont(monaco);
- TextSize(9);
- pi := ApproxPi(0);
- MoveTo(12, 24);
- drawstring('Initial value: ');
- DrawPi(pi);
- pi := ApproxPi(1);
- MoveTo(12, 36);
- drawstring('First iteration: ');
- DrawPi(pi);
- pi := ApproxPi(2);
- MoveTo(12, 48);
- drawstring('Second iteration: ');
- DrawPi(pi);
- if Button then
- repeat until not Button;
- repeat until Button;
- repeat until not Button;
- FlushEvents(everyEvent, 0);
- DisposeWindow(window);
- end;
- end.