Print Thread
AsFloat vs. AsCurrency
#7240 03/03/05 09:27 PM
Joined: Feb 2005
Posts: 49
A
Member
OP Offline
Member
A
Joined: Feb 2005
Posts: 49
Hello,
Using D7, DOA 4.0.5.0, Oracle 9.01.

A TOracleDataSet (ODS) read 2 columns (COL1, COL2) of NUMBER type. The corrispondent TField is a TFloatField.
Suppose COL1 and COL2 contains the same decimal value : the following test
ODS.FieldByName('COL1').AsFloat = ODS.FieldByName('COL2').AsFloat
return TRUE, correctly.
Then, I edit and post a record, replacing the same value in COL2.
After it, testing COL1 and COl2 as above, the result is FALSE.
I verified that the incorrect behavior happens after the TOracleDataset has been posted, while after a refresh it works fine.
I verified too that if I do the same test using AsCurrency instead AsFloat, it works properly:
(ODS.FieldByName('COL1').AsCurrency = ODS.FieldByName('COL2').AsCurrency)

It seems a bug.... any idea?

Re: AsFloat vs. AsCurrency
#7241 03/04/05 06:58 PM
Joined: Aug 1999
Posts: 22,220
Member
Offline
Member
Joined: Aug 1999
Posts: 22,220
Floating point precision is not 100%, so you cannot compare floating point numbers like this. There is no exact representation of (for example) 1.42, only an approximation that is up to 15 digits accurate (for example 1.41999999999997).


Marco Kalter
Allround Automations
Re: AsFloat vs. AsCurrency
#7242 03/17/05 11:06 PM
Joined: Feb 2005
Posts: 49
A
Member
OP Offline
Member
A
Joined: Feb 2005
Posts: 49
sorry for reading so later...
OK, I known the limits of digital rappresentation, but the case show 2 different behaviors on the same datas, this is the problem...
Is it true that AsCurrency has less problems then AsFloat?

Thanks

Re: AsFloat vs. AsCurrency
#7243 03/18/05 06:05 PM
Joined: Aug 2004
Posts: 83
Ukraine
A
Member
Offline
Member
A
Joined: Aug 2004
Posts: 83
Ukraine
if abs(ODS.FieldByName('COL1').AsFloat-ODS.FieldByName('COL2').AsFloat) < 0.000001 then ...

Re: AsFloat vs. AsCurrency
#7244 03/18/05 07:44 PM
Joined: Aug 1999
Posts: 22,220
Member
Offline
Member
Joined: Aug 1999
Posts: 22,220
The AsCurrency property returns the rounded floating point value of a TFloatField. It does not have any of the floating point accuracy problems, because it is a fixed point data type. It is represented internally by a 64 bit integer.

In short: comparing AsCurrency values for equality is safe.


Marco Kalter
Allround Automations

Moderated by  support 

Link Copied to Clipboard
Powered by UBB.threads™ PHP Forum Software 7.7.4
(Release build 20200307)
Responsive Width:

PHP: 7.1.33 Page Time: 0.038s Queries: 13 (0.019s) Memory: 2.5160 MB (Peak: 3.0427 MB) Data Comp: Off Server Time: 2024-05-18 20:21:55 UTC
Valid HTML 5 and Valid CSS