Numeric - float, rounding problem

The alpha/beta tester's forum for ZeosLib 7.0.x series

Report problems concerning our Delphi 2009+ version and new Zeoslib 7.0 features here.

This is a forum that will be removed once the 7.X version goes into stable!!

Moderators: gto, EgonHugeist, olehs

Locked
boreas
Fresh Boarder
Fresh Boarder
Posts: 10
Joined: 29.08.2011, 10:21

Numeric - float, rounding problem

Post by boreas »

Hi all,
I am using the zeos 922stable, Delphi XE, firebird 2.5.
I have a numeric (10.4) field. When recording in this field have already signed up some numbers wrong.
Example
Correct -> Wrong
1.95 -> 1.9444,
0.18 -> 0.1799
0.85 -> 0.8499

Is this a bug? What would you recommend me to do.
Thanks
papelhigienico
Expert Boarder
Expert Boarder
Posts: 113
Joined: 06.10.2006, 14:41
Location: Chapecó - Santa Catarina
Contact:

Post by papelhigienico »

What's the field datatype?
boreas
Fresh Boarder
Fresh Boarder
Posts: 10
Joined: 29.08.2011, 10:21

Post by boreas »

Datatype float.
Wild_Pointer
Expert Boarder
Expert Boarder
Posts: 164
Joined: 18.03.2008, 13:03
Contact:

Post by Wild_Pointer »

hello,

are you sure you get the 1.95 -> 1.9444 ?? not 1.95 -> 1.9499 by chance?
boreas
Fresh Boarder
Fresh Boarder
Posts: 10
Joined: 29.08.2011, 10:21

Post by boreas »

i am sorry. yes 1.95 -> 1.9499
Wild_Pointer
Expert Boarder
Expert Boarder
Posts: 164
Joined: 18.03.2008, 13:03
Contact:

Post by Wild_Pointer »

the problem is due to the float representation 1.95 is in fact stored as 1.94999... The problem is that when writing it to DB it does not get rounded but truncated... I didn't notice that behavior on postgresql I'm working with so my guess is the problem is in interbase (firebird) part of zeos. Would you be able to find and fix it?
boreas
Fresh Boarder
Fresh Boarder
Posts: 10
Joined: 29.08.2011, 10:21

Post by boreas »

when i issue a direct command to DB via zsqlprocessor, there is no problem. I overcame this problem using this solution. However, I havent fount any other solution apart from this.
Wild_Pointer
Expert Boarder
Expert Boarder
Posts: 164
Joined: 18.03.2008, 13:03
Contact:

Post by Wild_Pointer »

Would you please try using BCD Fields instead Float fields?
boreas
Fresh Boarder
Fresh Boarder
Posts: 10
Joined: 29.08.2011, 10:21

Post by boreas »

thanks for the post.
as far I know, floating type is given by deplhi by default, I cannot change it. How may i convert it into BCD type?

I cannot use cast command, it doesnt allow me to modify (editing)

thanks in advance.
Wild_Pointer
Expert Boarder
Expert Boarder
Posts: 164
Joined: 18.03.2008, 13:03
Contact:

Post by Wild_Pointer »

hello

try deleting field and then adding it manually in the field list. When adding manually you can specify the field type. There is no way to convert a field from one type to another without recreating it. I'm not sure it will help (or work at all) but please experiment :)

Good luck!
papelhigienico
Expert Boarder
Expert Boarder
Posts: 113
Joined: 06.10.2006, 14:41
Location: Chapecó - Santa Catarina
Contact:

Post by papelhigienico »

Float precision (32 bits). This issue happens on others databases, with variables, devices. Double datatype (64 bits) can solve this.
Wild_Pointer
Expert Boarder
Expert Boarder
Posts: 164
Joined: 18.03.2008, 13:03
Contact:

Post by Wild_Pointer »

hello papelhigienico,

but float(32) bit should have at least 7 digit precision. This is more than needed for the example. Please explain in more detail what goes wrong here please.

Thank you in advance
boreas
Fresh Boarder
Fresh Boarder
Posts: 10
Joined: 29.08.2011, 10:21

Post by boreas »

when i define the field manually, system does not allow anything but float. when i define it as float i enter any number i want, like 0,6000 and post it, however when i refresh it, it is displayed as 0,5999. And this is even not valid for every number. For example if I post 1,1450, i can do that. the number that is logged into the data base and the number I stored are not the same. All process regarding post and delete require to use zsqlprocessor.

I found no other alternatives.

Thanks
papelhigienico
Expert Boarder
Expert Boarder
Posts: 113
Joined: 06.10.2006, 14:41
Location: Chapecó - Santa Catarina
Contact:

Post by papelhigienico »

Wild_Pointer, yes, I agree with this, but this don't occurs with some numbers... I don't know why.
andrevanzuydam
Zeos Dev Team
Zeos Dev Team
Posts: 32
Joined: 22.10.2005, 08:53
Location: Bloemfontein
Contact:

Confirmation of a bug with this.

Post by andrevanzuydam »

I have a system that uses up to 10 decimals in some cases and this bug caught me.

The problem lies in UpdateBigDecimal in ZDBCInterbase6Utils

Please can you upload the code for your function so that I can see if I have a fix for it, It is possible that I have not made a commit for this fix.
Image
Nothing is impossible!
Locked