Merge branch 'bigint' into day24-analytical

This commit is contained in:
Stefan Müller 2024-05-20 01:01:20 +02:00
commit d503968cee
1 changed files with 26 additions and 3 deletions

View File

@ -78,6 +78,8 @@ type
class property Zero: TBigInt read GetZero; class property Zero: TBigInt read GetZero;
function CompareTo(constref AOther: TBigInt): Integer; function CompareTo(constref AOther: TBigInt): Integer;
function TryToInt64(out AOutput: Int64): Boolean; function TryToInt64(out AOutput: Int64): Boolean;
// TODO: ToString is currently for debug output only.
function ToString: string;
class function FromInt64(const AValue: Int64): TBigInt; static; class function FromInt64(const AValue: Int64): TBigInt; static;
class function FromHexadecimalString(const AValue: string): TBigInt; static; class function FromHexadecimalString(const AValue: string): TBigInt; static;
class function FromBinaryString(const AValue: string): TBigInt; static; class function FromBinaryString(const AValue: string): TBigInt; static;
@ -237,10 +239,17 @@ class function TBigInt.SubtractAbsoluteValues(constref AA, AB: TBigInt; const AR
var var
a, b: TBigInt; a, b: TBigInt;
carry: Cardinal; carry: Cardinal;
i, j, lastNonZeroDigitIndex, len: Integer; compare, i, j, lastNonZeroDigitIndex, len: Integer;
begin begin
// Establishes the operand order, such that Abs(a) is not less than Abs(b). // Establishes the operand order, such that Abs(a) is not less than Abs(b).
if (AA.CompareToAbsoluteValues(AB) >= 0) then compare := AA.CompareToAbsoluteValues(AB);
if compare = 0 then
begin
Result := Zero;
Exit;
end;
if compare > 0 then
begin begin
a := AA; a := AA;
b := AB; b := AB;
@ -491,6 +500,21 @@ begin
end; end;
end; end;
function TBigInt.ToString: string;
var
i: Integer;
begin
if FIsNegative then
Result := '-'
else
Result := '';
for i := 0 to Length(FDigits) - 2 do
Result := Result + '(' + IntToStr(FDigits[i]) + ' + 2^32 * ';
Result := Result + IntToStr(FDigits[Length(FDigits) - 1]);
for i := 0 to Length(FDigits) - 2 do
Result := Result + ')';
end;
class function TBigInt.FromInt64(const AValue: Int64): TBigInt; class function TBigInt.FromInt64(const AValue: Int64): TBigInt;
var var
absVal: Int64; absVal: Int64;
@ -565,7 +589,6 @@ begin
Result := TBigInt.MultiplyAbsoluteValues(A, B, A.IsNegative <> B.IsNegative); Result := TBigInt.MultiplyAbsoluteValues(A, B, A.IsNegative <> B.IsNegative);
end; end;
// TODO: Shift operator could be implemented with a single Move call, but I do not want to change it without test cases.
operator shl(const A: TBigInt; const B: Integer): TBigInt; operator shl(const A: TBigInt; const B: Integer): TBigInt;
var var
i, j, digitShifts, bitShifts, reverseShift, len, newLength: Integer; i, j, digitShifts, bitShifts, reverseShift, len, newLength: Integer;