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.

Equal operator overload

Project: Fractions

Unit: DelphiDabbler.Lib.Fractions

Record: TFraction

Applies to: ~>0.1

class operator Equal(const A, B: TFraction): Boolean;

Description

This operator overload permits TFractions to be tested for equality using the = operator.

Two fractions are considered equal if they can be simplified to the same fraction. For example 1/2 = 2/4. Formally, two fractions are equal if their 1st and 2nd cross products are equal.

A TFraction can also be compared to integer and floating point types using the = operator. The Implicit operator takes care of converting the integer and floating point operands to TFraction before performing the comparison.

Example

var
  F1, F2, F3, F4: TFraction;
  D: Double;
  I: Integer;
begin
  F1 := TFraction.Create(1, 4);
  F2 := TFraction.Create(1, 4);
  F3 := TFraction.Create(2, 8);
  F4 := TFraction.Create(3, 1);
  D := 0.25;
  I := 3;
  Assert(F1 = F2);
  Assert(F1 = F3);
  Assert(D = F1);
  Assert(F1 = D);
  Assert(I = F4);
  Assert(F4 = I);
end;

See Also