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.

Add operator overload

Project: Fractions

Unit: DelphiDabbler.Lib.Fractions

Record: TFraction

Applies to: ~>0.1

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

Description

This operator overload permits two TFractions to be added together using the + operator.

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

Example

var
  A, B, C, Res: TFraction;
begin
  A := TFraction.Create(2, 3);
  B := TFraction.Create(5, 6);
  C := TFraction.Create(4, 6);
  Res := A + B;  // 2/3 + 5/6 = 4/6 + 5/6 = 9/6 = 3/2
  Assert((Res.Numerator = 3) and (Res.Denominator = 2));
  Res := 2 + B;  // 2 + 5/6 = 12/6 + 5/6 = 17/6
  Assert((Res.Numerator = 17) and (Res.Denominator = 6));
  Res := 1.5 + C; // 1.5 + 4/6 = 3/2 + 4/6 = 9/6 + 4/6 = 13/6
  Assert((Res.Numerator = 13) and (Res.Denominator = 6));
end;

See Also