Fractions:

- fix FloatToFraction() if Abs(Value) > 1
- fix bug in Resolve which resulted in altering the value of self


git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4392 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
lazarus-bart
2015-11-18 15:26:44 +00:00
parent c1261c8d39
commit 0f8ef4176e

View File

@@ -400,7 +400,7 @@ begin // find nearest fraction
//writeln('IntPart := ',IntPart); //writeln('IntPart := ',IntPart);
//Avoid call to TFraction.Normalize in Result := H + IntPart //Avoid call to TFraction.Normalize in Result := H + IntPart
Result := H; Result := H;
Result.Numerator := Result.Numerator+ (Result.Numerator * IntPart); Result.Numerator := Result.Numerator+ (Result.Denominator * IntPart);
if IsNeg then if IsNeg then
Result.Numerator := -Result.Numerator; Result.Numerator := -Result.Numerator;
//writeln('MF_FloatToFraction End.'); //writeln('MF_FloatToFraction End.');
@@ -618,7 +618,7 @@ operator = (F1: TFraction; F2: TFraction) B: Boolean;
begin begin
F1.Normalize; F1.Normalize;
F2.Normalize; F2.Normalize;
B := (F1.Numerator = F2.Numerator) and (F2.Denominator = F2.Denominator); B := (F1.Numerator = F2.Numerator) and (F1.Denominator = F2.Denominator);
end; end;
operator < (F1: TFraction; F2: TFraction) B: Boolean; operator < (F1: TFraction; F2: TFraction) B: Boolean;
@@ -782,16 +782,16 @@ end;
function TFraction.Resolve: String; function TFraction.Resolve: String;
var var
IntPart: Int64; Num, IntPart: Int64;
begin begin
Normalize; Normalize;
if (Abs(Numerator) > Abs(Denominator)) then if (Abs(Numerator) > Abs(Denominator)) then
begin begin
IntPart := Numerator div Denominator; IntPart := Numerator div Denominator;
Numerator := Numerator mod Denominator; Num := Numerator mod Denominator;
if (IntPart < 0) then Numerator := -Numerator; if (IntPart < 0) then Num := -Num;
if (Numerator <> 0) then if (Num <> 0) then
Result := IntToStr(IntPart) + #32 + ToString Result := IntToStr(IntPart) + #32 + IntToStr(Num) + FracSymbol + IntToStr(Denominator)
else else
Result := IntToStr(IntPart); Result := IntToStr(IntPart);
end end