El 06/10/18 a las 20:48, Graeme Geldenhuys escribió:
On 03/10/18 20:05, Santiago A. wrote:
I don't know why you want to compare two floats, but you'd better use
currency type.
I fully understand that. We do financial calculation up to 6 decimal
places, so can't use Currency data type.
6 decimals, no currency that's a problem ;-)

Our real issue was the different results using the same calculation. I
thought order of precedence would apply in all cases, but couldn't fully
understand the outcomes we received. But after reading Florian and
Bernd's replies I now understand. The other issue was that our
application is 32-bit, where 64-bit would not have had this issue.
With 64 bits, reaching a 6 decimals error is more difficult, but it is still an issue. In your example you have done a couple of operations and you have got an error of 1E-11. After a hundred of operations, you could reach the fatal 1E-6. Once I had a problem like that, or integers, or floats. The best is rounding a lot in intermediate results, rounding before comparing and specify clearly the order: i.e. if you add a list of items with a discount, you can get different results if you apply the discount to each item and sum, than if you sum the items and apply the discount to the total. will the difference be less then 1E-6? Depend on the numbers, and how many items you sum. So you must specify : "Discount will be applied to each item". 64bits is a lot of precision, but don't be overconfident, even in such case errors can skyrocket with divisions with small divisors and/or a lot of operations. Comparing to zero is always dangerous, you'd better round the number to 6 decimals before comparing.

--
--------
Saludos
Santiago A.

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to