Browse Source

* Patch from Laco to fix issue #39261

git-svn-id: trunk@49623 -
(cherry picked from commit 573917706fb5a5cffd76239e90ef62fdee041f68)
michael 4 years ago
parent
commit
7936203fab
2 changed files with 23 additions and 2 deletions
  1. 21 2
      packages/rtl-objpas/src/inc/fmtbcd.pp
  2. 2 0
      tests/test/units/fmtbcd/tfmtbcd.pp

+ 21 - 2
packages/rtl-objpas/src/inc/fmtbcd.pp

@@ -1277,6 +1277,25 @@ IMPLEMENTATION
       neg1,
       neg2 : Boolean;
 
+      // real/reduced precision if there are on left side insignificant zero digits
+      function BCDPrec(const BCD: tBCD): word;
+      var scale: word;
+      begin
+        Result := BCD.Precision;
+        scale := BCDScale(BCD);
+        i := Low(BCD.Fraction);
+        while (Result>0) and (Result>scale) do begin
+          // high nibble
+          if BCD.Fraction[i] shr 4 <> 0 then Exit;
+          Dec(Result);
+          if Result <= scale then Exit;
+          // low nibble
+          if BCD.Fraction[i] <> 0 then Exit;
+          Dec(Result);
+          Inc(i);
+        end;
+      end;
+
     begin
 {$ifndef bigger_BCD}
       neg1 := ( BCD1.SignSpecialPlaces AND NegBit ) <> 0;
@@ -1291,8 +1310,8 @@ IMPLEMENTATION
         _WHEN ( NOT neg1 ) AND neg2
           _THEN result := +1;
         _WHENOTHER
-          pr1 := BCD1.Precision;
-          pr2 := BCD2.Precision;
+          pr1 := BCDPrec(BCD1);
+          pr2 := BCDPrec(BCD2);
 {$ifndef bigger_BCD}
           pl1 := BCD1.SignSpecialPlaces AND PlacesMask;
           pl2 := BCD2.SignSpecialPlaces AND PlacesMask;

+ 2 - 0
tests/test/units/fmtbcd/tfmtbcd.pp

@@ -301,6 +301,8 @@ begin
   testBCDCompare(-100.1, 100.1, -1);
   testBCDCompare(-100.1, -100.2, 1);
   testBCDCompare(100, 100.1, -1);
+  testBCDCompare(DoubleToBcd(0), 0, 0);
+  testBCDCompare(CurrToBcd(0), 0, 0);
   testBCDCompare(CurrToBcd(0.01), CurrToBcd(0.001), 1); // BCD values with Precision<Scale
   testBCDCompare(CurrToBcd(0.01), 0.01, 0);