home *** CD-ROM | disk | FTP | other *** search
-
- Function RLScan(X : real; N : integer): real;
-
- {Written by Paul F. Hultquist. Found in May 1985 PC Tech Journal. }
- {This function scans the positive exponent N from right to left to determine }
- {a sequence of multiplications and squarings that produce X (real) to the }
- {power N (integer) in a near minimum number of multiplications. It is used }
- {as a function in the function PwrI, listed below. The algorithm is }
- {Algorithm A, page 442 Vol. 2, 2nd Ed. of Knuth: "The Art of Computer }
- {Programming: Seminumerical Algorithms", Addison-Wesley, 1981. }
-
- Var
- Y, Z : real;
- O : boolean;
- BigN : integer;
-
- Begin
- BigN := N;
- Y := 1.0;
- Z := X;
- While BigN > 0 do
- begin
- O := odd(BigN);
- BigN := BigN Div 2;
- If O then
- begin
- Y := Y * Z;
- RLScan := Y
- end;
- Z := Z * Z;
- end;
- End; {RLScan}
-
- Function PwrI(X : real; N : integer): real;
-
- {Written by Paul F. Hultquist. Found in May 1985 PC Tech Journal. }
- {PwrI performs the test necessary to eliminate the noncomputable cases of }
- {finding X (real) to the power N (integer). It calls upon function RLScan to}
- {do the actual computation after it has, for example, replaced a negative }
- {exponent by a positive one (it does a reciprocation after return from RLScan}
- {in that case). }
-
- Begin
- if (N > 0) then
- PwrI := RLScan(X,N)
- else
- if (X <> 0.0) and (N < 0) then
- begin
- N := -N;
- PwrI := 1.0/RLScan(X,N);
- end
- else
- if (N = 0) and (X <> 0) then
- PwrI := 1.0
- else
- if (N = 0) and (X = 0) then
- begin
- writeln('0 to the 0 power. Halt.');
- Halt;
- end
- else
- if (N < 0) and (X =0) then
- begin
- writeln('Division by zero. Halt.');
- Halt;
- end;
- End; {PwrI}