BcdDivide function behave differently in Delphi XE and XE2

DelphiDelphi XeDelphi Xe2

Delphi Problem Overview


FmtBcd.pas has been extensively revised rewritten in Delphi XE2. In one of my projects, I have a case that uses a division operation on two Bcd values, but the two versions yield different results. In the worst case, the Delphi XE2 may throw a Bcd overflow error.

Example: Running the following code in Delphi XE2 console apps:

var A, B, C, D: TBcd;
begin
  A := StrToBcd('1');
  B := StrToBcd('3');
  BcdDivide(A, B, C);
  WriteLn(BcdToStr(C));

  try
    BcdMultiply(C, C, D);
    WriteLn(BcdToStr(D));
  except
    on E: Exception do
      WriteLn(E.Message);
  end;

  ReadLn;
end.

Output of the above will be:

0.333333333333333333333333333333333333333333333333333333333333333
BCD overflow

The variable C contains a Bcd Value with 63 decimal places of specificity. Performing a second BcdMultiply operation on variable C will cause a Bcd overflow error.

However, to run the same code in Delphi XE yields the following result without any exception prompt:

0.3333333333
0.11111111108888888889

Could anyone please suggest a best-practice method for resolving this problem?

Delphi Solutions


Solution 1 - Delphi

The code in the question produces the expected output in XE2 update 4. Note that update 3 produces the bad output and so clearly the fix arrived with update 4. Also, XE3 produces the expected output.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChau Chee YangView Question on Stackoverflow
Solution 1 - DelphiDavid HeffernanView Answer on Stackoverflow