From 18de900a38f2d06da4265175b512dd2648939c61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Mon, 20 May 2024 00:56:22 +0200 Subject: [PATCH 1/3] Fixed BigInt subtraction for equal operands --- UBigInt.pas | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/UBigInt.pas b/UBigInt.pas index a49ea16..4c8505e 100644 --- a/UBigInt.pas +++ b/UBigInt.pas @@ -237,10 +237,17 @@ class function TBigInt.SubtractAbsoluteValues(constref AA, AB: TBigInt; const AR var a, b: TBigInt; carry: Cardinal; - i, j, lastNonZeroDigitIndex, len: Integer; + compare, i, j, lastNonZeroDigitIndex, len: Integer; begin // 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 a := AA; b := AB; From 9c951073d9ed5e8b4c683e883184b93f9ab878f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Mon, 20 May 2024 00:56:52 +0200 Subject: [PATCH 2/3] Removed irrelevant todo --- UBigInt.pas | 1 - 1 file changed, 1 deletion(-) diff --git a/UBigInt.pas b/UBigInt.pas index 4c8505e..7d22c07 100644 --- a/UBigInt.pas +++ b/UBigInt.pas @@ -572,7 +572,6 @@ begin Result := TBigInt.MultiplyAbsoluteValues(A, B, A.IsNegative <> B.IsNegative); 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; var i, j, digitShifts, bitShifts, reverseShift, len, newLength: Integer; From 2ca960f19c69e64e4cf5751476655df7d69b494f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Mon, 20 May 2024 00:59:40 +0200 Subject: [PATCH 3/3] Added TBigInt.ToString for debugging --- UBigInt.pas | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/UBigInt.pas b/UBigInt.pas index 7d22c07..5a31b72 100644 --- a/UBigInt.pas +++ b/UBigInt.pas @@ -78,6 +78,8 @@ type class property Zero: TBigInt read GetZero; function CompareTo(constref AOther: TBigInt): Integer; 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 FromHexadecimalString(const AValue: string): TBigInt; static; class function FromBinaryString(const AValue: string): TBigInt; static; @@ -498,6 +500,21 @@ begin 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; var absVal: Int64;