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 TruncateToMultiple(const F: TFraction): TFraction;
This method truncates the fraction to the nearest whole number multiple of the given fraction F in the direction of zero. It returns the result.
For example, if you truncate 11/16
to a whole number multiple of 1/8
the result is 5/8
.
This example displays a sequence of ranges of fraction that converge towards π
var
FRangeLo, FRangeHi, FMult, FPi: TFraction;
N: Integer;
begin
Memo1.Clear;
FPi := PI; // Pi is automatically converted to fraction
for N := 1 to 20 do
begin
FMult := TFraction.Create(1, N);
FRangeLo := FPi.TruncateToMultiple(FMult);
FRangeHi := FRangeLo + FMult; // this value will be > Pi
// Added FMult to FRangeHi simplifies answer, so we un-simplify it
FRangeHi := FRangeHi.Convert(FRangeLo.Denominator div FRangeHi.Denominator);
WriteLn(
Format(
'Pi in falls in range [%d/%d..%d/%d]',
[
FRangeLo.Numerator, FRangeLo.Denominator,
FRangeHi.Numerator, FRangeHi.Denominator
]
)
);
end;
end;