home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / TURB_INC.ZIP / POWER.INC < prev    next >
Encoding:
Text File  |  1985-12-28  |  1.6 KB  |  62 lines

  1. {================================================}
  2. function Power(Valu:real; Exponent:integer): real;
  3. {================================================}
  4. {
  5. WRITTEN BY:     Tryg Helseth
  6.                 Minneapolis, Minnesota
  7.  
  8.                 Revision:  1/2/85
  9.  
  10. PURPOSE:        Raise a floating point value to an integer exponent.
  11.                 Sucessive squaring of the input value gives the
  12.                 powers of 1, 2, 4, 8, ...  which are combined to
  13.                 give the desired power.
  14.  
  15.                 WARNING:  overflow is not trapped; the program will
  16.                 abort with a run time error of 01 if overflow occurs.
  17.  
  18. PARAMETERS:
  19.  
  20.      INPUT:     Valu     = Value to be raised to a power.
  21.                 Exponent = desired power.
  22.  
  23.     OUTPUT:     none
  24.  
  25. FUNCTION VALUE: Power = Valu**Exponent
  26.  
  27. }
  28.  
  29. var     ThisPwr : integer;
  30.           ptemp : real;
  31.          negate : boolean;
  32.  
  33. begin
  34.     { adjust for negative exponent }
  35.     if (Exponent >= 0) then
  36.        negate := false
  37.     else begin
  38.        Exponent := - Exponent;
  39.        negate := true
  40.     end;
  41.  
  42.     { initialize ptemp for odd or even power }
  43.     if odd(Exponent) then
  44.        ptemp := valu
  45.     else
  46.        ptemp := 1;
  47.  
  48.     { compute powers of 2 and higher }
  49.     ThisPwr := 2;
  50.     while ThisPwr <= Exponent do begin
  51.        valu := sqr(valu);
  52.        if (ThisPwr and Exponent) <> 0 then ptemp := ptemp * valu;
  53.        ThisPwr := ThisPwr shl 1;
  54.        end;
  55.  
  56.     { Adjust if negative, and assign value to function }
  57.     if negate and (ptemp <> 0) then
  58.        power := 1 / ptemp
  59.     else
  60.        power := ptemp
  61. end;
  62.