Page 1 of 1

Problem with FormatBcd

Posted: 11.03.2021, 23:59
by Michl
I see in viewtopic.php?f=50&t=131587&p=164419&hi ... CD#p164419, there is already a bug report for FPC FormatBcd. I have a similar problem, but not the same, so I create a new topic.

I changed a big project from old Zeos trunk version to current 8.0 patches branch. I use 64bit FPC 3.2.0 Lazarus Trunk on Windows 7 and communicate with PostgreSQL 10.

On a table I have a value NUMERIC. With this field I got a rangecheck error.

I debugged the issue and can see for a value 0,014 there this tBCD is set:

Code: Select all

  FillChar(BCD{%H-}, SizeOf(BCD), 0);
  BCD.Fraction[0] := 20;
  BCD.Precision := 2;
  BCD.SignSpecialPlaces := 3; 
With this value FormatBCD('#,###0.000', BCD) raise a range check error. If I use BCDToStr(BCD), the value is converted to a string.


If I use BCD := StrToBCD('0,014'), I can see this tBCD is set:

Code: Select all

  FillChar(BCD{%H-}, SizeOf(BCD), 0);
  BCD.Fraction[0] := 1;
  BCD.Fraction[1] := 64;
  BCD.Precision := 3;
  BCD.SignSpecialPlaces := 3;    
With this value FormatBCD('#,###0.000', BCD) and BCDToStr(BCD) are working fine.


If I use BCD := StrToBCD('0,14'), I can see this tBCD is set:

Code: Select all

  FillChar(BCD{%H-}, SizeOf(BCD), 0);
  BCD.Fraction[0] := 20;
  BCD.Precision := 2;
  BCD.SignSpecialPlaces := 2; 
This is mostly similar to first case and works for both functions FormatBCD('#,###0.000', BCD) and BCDToStr(BCD).


Now I don't know, if this is a bug in TZAbstractRODataset(DataSet).FResultSet.GetBigDecimal with a wrong comma position or is this BCD value valid and there is a bug in fpc FormatBCD or do I miss something?!


Simple test also a workaround patch added, so I can use Zeos for now.

Re: Problem with FormatBcd

Posted: 18.03.2021, 18:12
by EgonHugeist
Hello Michl,

thanks for the detailed infos. I have fixed the bug on Zeos side, see r7388:https://sourceforge.net/p/zeoslib/code-0/7388/.
OTH it's definitelly a bug on FPC side too. Scale(SignSpecialPlaces) can't be greater than precision. Thus i would argue FPC FormatBCD is correct implemented wheras StrToBCD seems to be buggy (accept the FPC team argues "it's a feature" -> then FormatBCD should behave equal).

So please update from SVN and have fun! Next time please open a ticket on https://sourceforge.net/p/zeoslib/tickets/

Re: Problem with FormatBcd

Posted: 25.03.2021, 09:35
by Michl
Hello EgonHugeist,

thank you very much for your fast response and your hints! Sorry for late reply, I was struggling with a other issue, so I couldn't test this.

[Edit]
Next problem is fixed in Trunk! So you can ignore it!
[/Edit]


Not sure, if I should now open a bug tracker issue or post here?!

Now the app is starting correct and there is no exception anymore. But I encounter a new problem, IRC that wasn't before:

As I wrote above, I use

Code: Select all

GridData.Columns.Items[i].DisplayFormat := '#,###0.000';   
As I can see, this works fine for values <1 or >= 1000.
Values between this, show the thousend separator in front of it.

E.G.:
.026,000
.028,150
.013,000
.256,000
.001,325

When I debug the issue for e.g. 26 there the tBCD is set:

Code: Select all

  BCD.Fraction[0] := 0;
  BCD.Fraction[1] := 38;
  BCD.Precision := 4;
  BCD.SignSpecialPlaces := 0;    
, but should be:

Code: Select all

  BCD.Fraction[0] := 0;
  BCD.Fraction[1] := 38;
  BCD.Precision := 2;
  BCD.SignSpecialPlaces := 0;    
or

28,15:

Code: Select all

  BCD.Fraction[0] := 0;
  BCD.Fraction[1] := 40;
  BCD.Fraction[2] := 21;
  BCD.Precision := 6;
  BCD.SignSpecialPlaces := 2;    
FPC said, it should be:

Code: Select all

  BCD.Fraction[0] := 40;
  BCD.Fraction[1] := 21;
  BCD.Precision := 4;
  BCD.SignSpecialPlaces := 2;    

Re: Problem with FormatBcd

Posted: 25.03.2021, 10:23
by Michl
Hello EgonHugeist,

just tested trunk version. There is now everything fine!!

Thank you very much!