Welcome to the new DelphiDabbler Code Library Documentation.

This is a new site that's currently running on alpha code. There are going to be bugs. If you discover any, please report them on the site's issues page (GitHub account required). Thanks.

Warning: Many URLs are going to change. Refer to the README file to discover which library project's documentation has been completed.

Divide operator overload

Project: Fractions

Unit: DelphiDabbler.Lib.Fractions

Record: TFraction

Applies to: ~>0.1

class operator Divide(const A, B: TFraction): TFraction;

Description

This operator overload permits one TFraction to be divided by another using the / operator.

The result of the division is returned as a new TFraction instance, reduced to its lowest terms.

One of the operands may be either an integer or floating point value. The Implicit operator takes care of converting the integer and floating point operands to a TFraction before performing the division.

The second operand must not be zero. In the case of a TFraction this means the Numerator must not be zero.

Example

var
  F1, F2, FRes: TFraction;
begin
  // 2/3 / 5/6 = 2/3 * 6/5 = 12/15 = 4/5
  F1 := TFraction.Create(2, 3);
  F2 := TFraction.Create(5, 6);
  FRes := F1 / F2;
  Assert((FRes.Numerator = 4) and (FRes.Denominator = 5));
  // 5/8 / 3 = 5/8 * 1/3 = 5/24
  F1 := TFraction.Create(5, 8);
  FRes := F1 / 3;
  Assert((FRes.Numerator = 5) and (FRes.Denominator = 24));
  // 2.5 / 3/4 = 5/2 / 3/4 = 5/2 * 4/3 = 20/6 = 10/3
  Assert((FRes.Numerator = 10) and (FRes.Denominator = 3));
end;

See Also