ESBIntPower Routines
Raises Values to an Integer Power.

Unit
QESBPCSMath

Overloaded Variants
Function ESBIntPower(const X: LongInt; const N: Byte): Int64;
Function ESBIntPower(const X: Extended; const N: LongInt): Extended;
Function ESBIntPower(const X: Extended; const N: LongWord): Extended;

Declaration
Function ESBIntPower(const X: LongInt; const N: Byte): Int64;

Description
The different overloaded routines allow for better precision depending on the data types involved. The LongInt/Byte is for strictly Integer operations.

Zero to a Negative power raises an Exception.

The LongInt/Byte Routine cannot handle N > 62, as this raises an exception, so for N > 63, take the longint value and multiply it by 1.0 to ensure a different routine gets called.

Thanks to Rory Daulton for improvements.

Parameters
Value to use as the Base
Integer Value to raise the Base to

Category
Arithmetic Routines for Integers
Arithmetic Routines for Floats

Implementation

function ESBIntPower (const X: LongInt; const N: Byte): Int64; overload;
var
     I: LongWord;
begin
     if N > 62 then
          raise EMathError.Create (rsPowerInt64);

     if N = 0 then
          Result := 1
     else if (X = 0) or (X = 1) then
          Result := X
     else if X = -1 then
     begin
          if Odd (N) then
               Result := -1
          else
               Result := 1
     end
     else
     begin
          Result := X;
          for I := 2 to N do
               Result := Result * X;
     end;
End;

Declaration
Function ESBIntPower(const X: Extended; const N: LongInt): Extended;

Implementation

function ESBIntPower (const X: Extended; const N: LongInt): Extended; overload;
var
     P: LongWord;
begin
     if N = 0 then
          Result := 1
     else if (X = 0) then
     begin
          if N < 0 then
               raise EMathError.Create (rsZeroToNegPower)
          else
               Result := 0
     end
     else if (X = 1) then
          Result := 1
     else if X = -1 then
     begin
          if Odd (N) then
               Result := -1
          else
               Result := 1
     end
     else if N > 0 then
          Result := IntPow (X, N)
     else
     begin
          if N <> Low (LongInt) then
               P := abs (N)
          else
               P := LongWord (High (LongInt)) + 1;

          try
               Result := IntPow (X, P);
          except
               on EMathError do
               begin
                    Result := IntPow (1 / X, P); { try again with another method, }
                    Exit; {   perhaps less precise         }
               end {on};
          end {try};
          Result := 1 / Result;
     end;
End;

Declaration
Function ESBIntPower(const X: Extended; const N: LongWord): Extended;

Implementation

function ESBIntPower (const X: Extended; const N: LongWord): Extended; overload;
begin
     if N = 0 then
          Result := 1
     else if (X = 0) or (X = 1) then
          Result := X
     else if X = -1 then
     begin
          if Odd (N) then
               Result := -1
          else
               Result := 1
     end
     else
          Result := IntPow (X, N)
End;


HTML generated by Time2HELP
http://www.time2help.com