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.
Project: Fractions
Unit: DelphiDabbler.Lib.Fractions
Record: TFraction
Applies to: ~>0.1
class operator Modulus(const A, B: TFraction): TFraction;
This operator overload permits the mod
operator to be applied to TFraction operands. It calculates the remainder after dividing A by B.
The result of the operation 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 modulus operation.
The second operand must not be zero. In the case of a TFraction this means the Numerator must not be zero.
var
F1, F2, FRes: TFraction;
begin
// 7/8 mod 1/3 = 7/8 - 2 * 1/3 = 7/8 - 2/3 = 21/24-16/24 = 5/24
F1 := TFraction.Create(7, 8);
F2 := TFraction.Create(1, 3);
FRes := F1 mod F2;
Assert(FRes.Numerator = 5) and (FRes.Denominator = 24));
// 4/3 mod 2/3 = 4/3 - 2 * 2/3 = 4/3 - 4/3 = 0
F1 := TFraction.Create(4, 3);
F2 := TFraction.Create(2, 3);
FRes := F1 mod F2;
Assert(FRes.Numerator = 0) and (FRes.Denominator = 1));
// 32/5 mod 3 = 32/5 - 2 * 3 = 32/5 - 6/1 = 32/5 - 30/5 = 2/5
F1 := TFraction.Create(32, 5);
FRes := F1 mod 3;
Assert(FRes.Numerator = 2) and (FRes.Denominator = 5));
// 5 mod 2/3 = 5 - 7 * 2/3 = 5/1 - 14/3 = 15/3 - 14/3 = 1/3
F2 := TFraction.Create(2, 3);
FRes := 5 mod F2;
Assert(FRes.Numerator = 1) and (FRes.Denominator = 3));
// 10 7/8 mod 3.75 = 87/8 mod 15/4 = 87/8 mod 30/8 = 87/8 - 60/8 = 27/8
F1 := TFraction.Create(87, 8);
FRes := F1 mod 3.75;
Assert(FRes.Numerator = 27) and (FRes.Denominator = 8));
end;