home *** CD-ROM | disk | FTP | other *** search
- {================================================}
- function Power(Valu:real; Exponent:integer): real;
- {================================================}
- {
- WRITTEN BY: Tryg Helseth
- Minneapolis, Minnesota
-
- Revision: 1/2/85
-
- PURPOSE: Raise a floating point value to an integer exponent.
- Sucessive squaring of the input value gives the
- powers of 1, 2, 4, 8, ... which are combined to
- give the desired power.
-
- WARNING: overflow is not trapped; the program will
- abort with a run time error of 01 if overflow occurs.
-
- PARAMETERS:
-
- INPUT: Valu = Value to be raised to a power.
- Exponent = desired power.
-
- OUTPUT: none
-
- FUNCTION VALUE: Power = Valu**Exponent
-
- }
-
- var ThisPwr : integer;
- ptemp : real;
- negate : boolean;
-
- begin
- { adjust for negative exponent }
- if (Exponent >= 0) then
- negate := false
- else begin
- Exponent := - Exponent;
- negate := true
- end;
-
- { initialize ptemp for odd or even power }
- if odd(Exponent) then
- ptemp := valu
- else
- ptemp := 1;
-
- { compute powers of 2 and higher }
- ThisPwr := 2;
- while ThisPwr <= Exponent do begin
- valu := sqr(valu);
- if (ThisPwr and Exponent) <> 0 then ptemp := ptemp * valu;
- ThisPwr := ThisPwr shl 1;
- end;
-
- { Adjust if negative, and assign value to function }
- if negate and (ptemp <> 0) then
- power := 1 / ptemp
- else
- power := ptemp
- end;
-