67
votes

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?

1
Hard to say anything else except that this is a bug in XE2.Clint Good
Same results when compiled for Win32 or Win64?Francesca
@François: Both Win32 and Win64 in XE2 produce same result.Chau Chee Yang
Sorry to ask but what's your question here? You're just making a statement. If the supplied lib is buggy then you can simply reimplement the used functions. Or use the ones from the working version. Whatever.bidifx

1 Answers

6
votes

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.