XtoY Function
Returns X^Y - handles all cases (except those mentioned below).

Unit
QESBPCSMath

Declaration
Function XtoY(const X, Y: Extended): Extended;

Description
Zero to a Negative power raises an Exception.

If the routine can be handled with ESBIntPower then it is used internally.

Thanks to Rory Daulton for improvements.

Parameters
Value to use as Base.
Value to use as Power.

Returns
X^Y.

Category
Arithmetic Routines for Floats

Implementation

function XtoY (const X, Y: Extended): Extended;

function PowerAbs: Extended; // Routine developed by Rory Daulton
     var
          ExponentPow2: Extended; // equivalent exponent to power 2
     begin
          try
               ExponentPow2 := ESBLog2 (Abs (X)) * Y;
          except
               on EMathError do
                    // allow underflow, when ExponentPow2 would have been negative
                    if (Abs (X) > 1) <> (Y > 0) then
                    begin
                         Result := 0;
                         Exit;
                    end {if}
                    else
                         raise;
          end {try};
          Result := Pow2 (ExponentPow2);
     end;
begin
     if FloatIsZero (Y) then
          Result := 1
     else if FloatIsZero (X) then
     begin
          if Y < 0 then
               raise EMathError.Create (rsZeroToNegPower)
          else
               Result := 0
     end
     else if FloatIsZero (Frac (Y)) then
     begin
          if (Y >= Low (LongInt)) and (Y <= High (LongInt)) then
               Result := ESBIntPower (X, LongInt (Round (Y)))
          else
          begin
               if (X > 0) or FloatIsZero (Frac (Y / 2.0)) then
                    Result := PowerAbs
               else
                    Result := -PowerAbs
          end;
     end
     else if X > 0 then
          Result := PowerAbs
     else
          raise EMathError.Create (rsInvalidXtoY)
End;


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