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.

Subtract operator overload

Project: Fractions

Unit: DelphiDabbler.Lib.Fractions

Record: TFraction

Applies to: ~>0.1

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

Description

This operator overload permits one TFraction to be subtracted from another using the - operator.

The result of the subtraction 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 subtraction.

Example

var
  F1, F2, FRes: TFraction;
begin
  F1 := TFraction.Create(2, 3);
  F2 := TFraction.Create(5, 6);
  FRes := F1 - F2;  // 2/3 - 5/6 = 4/6 - 5/6 = -1/6
  Assert((FRes.Numerator = -1) and (FRes.Denominator = 6));
  F2 := TFraction.Create(-5, 6);
  FRes := 1 - F2;   // 1 - -5/6 = 6/6 - -5/6 = 11/6
  Assert((FRes.Numerator = 11) and (FRes.Denominator = 6));
  F1 := TFraction.Create(3, 4);
  FRes := F1 - 0.25;  // 3/4 - 0.25 = 3/4 - 1/4 = 2/4 = 1/2
  Assert((FRes.Numerator = 1) and (FRes.Denominator = 2));
end;

See Also