IntDivide operator overload

Project: Fractions

Unit: DelphiDabbler.Lib.Fractions

Record: TFraction

Applies to: ~>0.1

class operator IntDivide(const A, B: TFraction): Int64;

Description

This operator overload permits an integer division to be performed on TFractions using the div operator.

The result of the operation is the largest whole number less or equal to the result of dividing A by B.

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 integer division.

The second operand must not be zero. In the case of a TFraction this means the Numerator must not be zero.

Example

var
  F1, F2: TFraction;
begin
  // 7/8 div 1/3 = Trunc(7/8 * 3/1) = Trunc(21/8) = 2
  F1 := TFraction.Create(7, 8);
  F2 := TFraction.Create(1, 3);
  Assert(F1 div F2 = 2);

  // 2/3 div 2/3 = Trunc(2/3 * 3/2) = Trunc(6/6) = 1
  F1 := TFraction.Create(2, 3);
  F2 := TFraction.Create(2, 3);
  Assert(F1 div F2 = 1);

  // 5 div 2/3 = Trunc(5/1 * 3/2) = Trunc(15/2) = 7
  F2 := TFraction.Create(2, 3);
  Assert(5 div F2 = 7);

  // 10/3 div 1 = Trunc(10/3 * 1/1) = Trunc(10/3) = 3
  F1 := TFraction.Create(10, 3);
  Assert(F1 div 1 = 3);
end;

See Also