Project: Fractions
Unit: DelphiDabbler.Lib.Fractions
Record: TFraction
Applies to: ~>0.1
class operator Add(const A, B: TFraction): TFraction;
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.
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;