AsFloat vs. AsCurrency

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?
 
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).
 
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
 
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.
 
Back
Top