[fpc-devel] Rounding inconsistent (critical)

Vincent Snijders vsnijders at quicknet.nl
Mon Oct 2 11:39:56 CEST 2006


Graeme Geldenhuys schreef:
> Hi,
> 
> I created the follow fpcUnit tests.  For those that don't know
> fpcUnit, the AssertEquals takes three parameters: Failing message,
> Expected Value, Actual Value.
> 
> Now from the tests below, I am formatting all values to 2 decimal
> values.  I expected all tests to pass, but it doesn't. Test 9 fails
> and said it expected 0.00 which is wrong.
> 
> Rules for rounding.  If a decimal is exactly halfway (5) or more,
> rounding goes up. If decimal is < 5, rounding goes down.  This rules
> is proven by tests 5, 6, 7 and 8.  So why is test 9 failing?  Is this
> a bug?
> 
>  AssertEquals('Failing on 1', '0.10', FormatFloat('0.00', 0.1));
>  AssertEquals('Failing on 2', '0.01', FormatFloat('0.00', 0.01));
>  AssertEquals('Failing on 3', '0.00', FormatFloat('0.00', 0.001));
>  AssertEquals('Failing on 4', '0.50', FormatFloat('0.00', 0.5));
>  AssertEquals('Failing on 5', '0.01', FormatFloat('0.00', 0.0051));
>  AssertEquals('Failing on 6', '0.05', FormatFloat('0.00', 0.049));
>  AssertEquals('Failing on 7', '0.00', FormatFloat('0.00', 0.0049));
>  AssertEquals('Failing on 8', '0.05', FormatFloat('0.00', 0.045));
>  AssertEquals('Failing on 9', '0.01', FormatFloat('0.00', 0.005));

It is not a bug, it is using bankers rounding. See 
http://lazarus-ccr.sourceforge.net/docs/rtl/system/round.html

Vincent



More information about the fpc-devel mailing list