home *** CD-ROM | disk | FTP | other *** search
- program x_to_n_time;
- { This program demonstrates the time differences between a number }
- { of the standard exponential functions and the x_to_n function. }
- { Modified and expanded by Claire A. Rinehart 1/6/86 }
-
- Var
- Iy,n : integer;
- y,t1,t2 : real;
- { These results were obtained on an IBM PCjr }
- { x type n type result type time (sec) }
- {$I time.inc }
- {$I xton.inc } { real integer real 2.31 }
- { power.inc } { real integer real 6.65 }
- { Ipower.inc } { integer integer integer 1.65 }
- { Ixton.inc } { integer integer integer 0.10 }
- { Rpower.inc } { real integer real 15.77 }
- { Mpower.inc } { real integer real 14.55 }
- {$I PwrR.inc } { real real real 6.76 }
- {$I PwrI.inc } { real integer real 2.86 }
-
- Function IXtoN(x : integer; n : integer) : integer;
- { This include file contains the x_to_n function that computes }
- { the Nth power of an integer number, where N is an integer. }
- { Author : Herb Holden }
- { Date : 11/17/84 }
- { Application : All systems. }
- { }
- { This function was taken from TUG newsletter vol I, issue 5 }
- { pg. 32. }
- { Modified to be a pure integer function by Claire A. Rinehart }
-
- Var
- p : integer;
- y : real;
- Begin {computes x to the nth power}
- if x = 0 then
- begin
- if n = 0 then
- begin {0 to the 0 is undefined}
- writeln('** error in IxTOn: x=0, n=0 **');
- y := 1/0
- end
- else
- IxTOn := 0 {0 to the n is 0}
- end
- else
- begin
- if n < 0 then {x to -n is (1/x) to n and can not be assigned to an integer variable}
- begin
- writeln('ERROR!! Attempted to assign a fraction to an integer.');
- y := 1/0;
- end;
- if odd(n) then {prepare for while loop}
- p := x
- else
- p := 1; {x to 0 will be 1}
- n := n shr 1;
- while n <> 0 do
- begin
- x := sqr(x);
- if odd(n) then p := p * x;
- n := n shr 1
- end;
- IxTOn := p
- end
- end; {IxTOn}
- function Ipwr(num,expon : integer): integer;
- {Use this function with care. Numbers between 32 and 64 k are represented }
- {as their negative counterparts because turbo handles integers as unsigned }
- {values in the following manner: 0, 1,...,32766,32767,-32768,-32767,...,-2,-1}
- {This can be confusing because a negative integer raised to an odd power will }
- {also generate a negative result. }
- Var
- y : real;
- begin
- if expon < 0 then
- begin
- writeln('Error!! Exponent can not be negative');
- y := 1/0;
- end;
- if expon = 0 then Ipwr :=1
- else
- Ipwr := num * Ipwr(num, expon - 1);
- end; {Ipwr}
- function pwr(num : real; expon : integer): real;
- begin
- pwr := exp(expon * ln(num));
- end; {pwr}
-
- Begin
- t1 := time;
- for n := 1 to 100 do
- y := xton(1.1,n); {5.5 sec}
- t2 := time;
- writeln('Time for xton: ',t2-t1:6:2);
-
- t1 := time;
- for n := 1 to 100 do
- y := pwr(1.1,n);
- t2 := time;
- writeln('Time for pwr: ',t2-t1:6:2);
-
- t1 := time;
- for n := 1 to 100 do
- Iy := Ipwr(1,n);
- t2 := time;
- writeln('Time for Ipwr: ',t2-t1:6:2);
-
- t1 := time;
- for n := 1 to 100 do
- Iy := IxTOn(1,n);
- t2 := time;
- writeln('Time for IxTOn: ',t2-t1:6:2);
-
- t1 := time;
- for n := 1 to 100 do
- y := Rpwr(1.1,n);
- t2 := time;
- writeln('Time for Rpwr: ',t2-t1:6:2);
-
- t1 := time;
- for n := 1 to 100 do
- y := Mpwr(1.1,n);
- t2 := time;
- writeln('Time for Mpwr: ',t2-t1:6:2);
-
- t1 := time;
- for n := 1 to 100 do
- y := Pwrr(1.1,n+0.1);
- t2 := time;
- writeln('Time for PwrR: ',t2-t1:6:2);
-
- t1 := time;
- for n := 1 to 100 do
- y := PwrI(1.1,n);
- t2 := time;
- writeln('Time for PwrI: ',t2-t1:6:2);
- end.