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
function CompareTo(const F: TFraction): TValueRelationship;
This method compares the current fraction instance with the TFraction given by the F. A value is returned that indicates the relationship of the two fractions. The values returned are:
EqualsValue, GreaterThanValue and LessThanValue are defined in the Types unit and have integer values 0, 1 and -1 respectively.
This method can also be used to compare an integer or a floating point to the current fraction simply by passing the integer or floating point value as the F parameter. The Implicit operator takes care of converting the integer and floating point parameter to a TFraction before performing the comparison.
Fractions are compared by calculating and comparing the 1st and 2nd cross products of the fractions. If the 1st cross product of two fractions, A and B, is less than the 2nd cross product then fraction A is less than fraction B. If the 1st cross product is greater then the second cross product then fraction A is greater than B. If the two cross products are the same then the fractions are equal.
This means than two fractions that appear to be different may in fact compare equal, for example the 1st and 2nd cross products of 3/4
and 6/8
are both 24
, so 3/4
= 6/8
.
var
F, F1, F2, F3: TFraction;
begin
F := TFraction.Create(7, 4);
F1 := TFraction.Create(1, 2);
F2 := TFraction.Create(14, 8);
F3 := TFraction.Create(25, 7);
Assert(F.CompareTo(F1) = GreaterThanValue); // 7/4 > 1/2
Assert(F.CompareTo(F2) = EqualsValue); // 7/4 = 14/8
Assert(F.CompareTo(F3) = LessThanValue); // 7/4 < 25/7
Assert(F.CompareTo(0.5) = GreaterThanValue); // 7/4 > 0.5
Assert(F.CompareTo(1.75 = EqualsValue); // 7/4 = 1.75
Assert(F.CompareTo(4.76 = LessThanValue); // 7/4 < 4.76
Assert(F.CompareTo(1) = GreaterTanValue); // 7/4 > 1
Assert(F.CompareTo(2) = LessThanValue); // 7/4 < 2
end;