Power class method

Project: Fractions

Unit: DelphiDabbler.Lib.Fractions

Record: TFraction

Applies to: 0.1.x

class function Power(const F: TFraction; Exponent: ShortInt): TFraction; static;

Applies to: ~>0.2

class function Power(const F: TFraction; Exponent: ShortInt): TFraction; 
  overload; static;
function Power(const Exponent: ShortInt): TFraction; overload;

Description

Return a fraction raised to an integer power specified by the Exponent parameter. The resulting fraction is simplified.

Exponent can be positive, zero or negative except unless the fraction is zero when Exponent must be non-negative.

When Exponent is negative the reciprocal of F is raised to the power of the absolute value of Exponent. This is because, for any number n, n^-x = 1/(N^x).

The class method takes the fraction to be operated on, F, as its first parameter.

[~>0.2] The instance method operates on the fraction instance upon which it is called.

Examples

Class method examples

Here’s a simple function that squares the given fraction.

function Square(const F: TFraction): TFraction;
begin
  Result := TFraction.Power(F, 2);
end;

[~>0.2] Here is some code that write out all the integer powers of 1/2 from -32 to +32:

var
  F, G: TFraction;
  N: ShortInt;
begin
  F := TFraction.Create(1, 2);  // 1/2
  for N := -32 to 32 do
  begin
    G := F.Power(N);
    WriteLn(
      Format(
        '1/2^%d = %d/%d', [N, G.Numerator, G.Denominator]
      )
    );
  end;
end;

See Also