Ignore:
Timestamp:
Nov 16, 2023, 2:01:59 PM (6 months ago)
Author:
chronos
Message:
  • Modified: BigInt implementation improvements.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ByteArray/BigInt.pas

    r51 r52  
    4747    class operator GreaterThanOrEqual(A, B: TBigInt): Boolean;
    4848    procedure Shrink;
     49    function IsNegative: Boolean;
     50    function IsZero: Boolean;
     51    function IsPositive: Boolean;
    4952    procedure SetByteArray(AData: TArrayOfByte; ASize: TBigIntSize);
    5053    procedure GetByteArray(var AData: TArrayOfByte; ASize: TBigIntSize);
    5154    function Copy(Size: TBigIntSize): TBigInt;
    52     property Size: Byte read GetSize write SetSize;
     55    property Size: TBigIntSize read GetSize write SetSize;
    5356    property Data[Index: TBigIntSize]: Byte read GetData write SetData;
    5457  end;
     
    8588procedure TBigInt.SetData(Index: TBigIntSize; AValue: Byte);
    8689begin
    87   if Index > Size - 1 then Size := Index + 1;
     90  if Index > Integer(Size) - 1 then Size := Index + 1;
    8891  FData[Index] := AValue;
    8992end;
     
    241244  Carry: Byte;
    242245begin
    243   Result.Size := A.Size;
     246  Result.Size := Max(A.Size, B.Size);
    244247  Carry := 0;
    245248  I := 0;
     
    259262begin
    260263  Result := A.Size = B.Size;
     264  if not Result then Exit;
    261265  for I := 0 to A.Size - 1 do
    262266    if A.FData[I] <> B.FData[I] then begin
     
    271275begin
    272276  Result := A.Size = B.Size;
     277  if not Result then Exit;
    273278  for I := 0 to A.Size - 1 do
    274279    if A.FData[I] <> B.FData[I] then begin
     
    280285
    281286class operator TBigInt.LessThan(A, B: TBigInt): Boolean;
    282 begin
    283 
     287var
     288  Temp: TBigInt;
     289begin
     290  Temp := A - B;
     291  Result := Temp.IsNegative;
    284292end;
    285293
    286294class operator TBigInt.LessThanOrEqual(A, B: TBigInt): Boolean;
    287 begin
    288 
     295var
     296  Temp: TBigInt;
     297begin
     298  Temp := A - B;
     299  Result := Temp.IsNegative or Temp.IsZero;
    289300end;
    290301
    291302class operator TBigInt.GreaterThan(A, B: TBigInt): Boolean;
    292 begin
    293 
     303var
     304  Temp: TBigInt;
     305begin
     306  Temp := A - B;
     307  Result := Temp.IsPositive and not Temp.IsZero;
    294308end;
    295309
    296310class operator TBigInt.GreaterThanOrEqual(A, B: TBigInt): Boolean;
    297 begin
    298 
     311var
     312  Temp: TBigInt;
     313begin
     314  Temp := A - B;
     315  Result := Temp.IsPositive;
    299316end;
    300317
     
    307324    Dec(I);
    308325  Size := I + 1;
     326end;
     327
     328function TBigInt.IsNegative: Boolean;
     329begin
     330  Result := (FData[Size - 1] and $80) = $80;
     331end;
     332
     333function TBigInt.IsZero: Boolean;
     334var
     335  I: TBigIntSize;
     336begin
     337  Result := True;
     338  for I := 0 to Size - 1 do
     339    if FData[I] <> 0 then begin
     340      Result := False;
     341      Break;
     342    end;
     343end;
     344
     345function TBigInt.IsPositive: Boolean;
     346begin
     347  Result := (FData[Size - 1] and $80) = 0;
    309348end;
    310349
     
    329368    4: PDWord(@Result.FData[0])^ := PDWord(@FData[0])^;
    330369    8: PQWord(@Result.FData[0])^ := PQWord(@FData[0])^;
     370    else Move(PByte(@FData[0])^, PByte(@Result.FData[0])^, Size);
    331371  end;
    332372end;
Note: See TracChangeset for help on using the changeset viewer.