in reply to sprintf and decimals
sprintf uses "Banker's rounding". ( Joost points out that this may be compiler-dependant. I'm using ActivePerl. )
4 < 5, so 2.674 rounds to 2.67
6 > 5, so 2.676 rounds to 2.68
5 = 5, and 7 is odd, so 2.675 rounds to 2.67
5 = 5, and 8 is even, so 2.685 rounds to 2.69
Banker's rounding (the even/odd bit) helps in the case of where one is adding a lot of rounded .5's
>perl -e "print 10.5 + 15.5 + 25.5" 51.5 (Unrounded) >perl -e "print 11 + 16 + 26" 53 (Conventional, delta = 1.5) >perl -e "print 10 + 16 + 26" 52 (Banker's, delta = 0.5)
Of course, it doesn't help in the case where one is adding lots of rounded .6's, but it doesn't hurt either.
>perl -e "print 10.6 + 15.6 + 25.6" 51.8 (Unrounded) >perl -e "print 11 + 16 + 26" 53 (Conventional, delta = 1.2) >perl -e "print 11 + 16 + 26" 53 (Banker's, delta = 1.2)
While it can produce a different result when the data is "well" distributed, it doesn't hurt there either.
>perl -e "print 0.0+0.1+0.2+0.3+0.4+0.5+0.6+0.7+0.8+0.9" 4.5 (Unrounded) >perl -e "print 0+0+0+0+0+1+1+1+1+1" 5 (Conventional, delta = 0.5) >perl -e "print 0+0+0+0+0+0+1+1+1+1" 4 (Banker's, delta = 0.5)
Update: Added background on banker's rounding.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: sprintf and decimals
by Joost (Canon) on Nov 15, 2006 at 18:54 UTC | |
by ikegami (Patriarch) on Nov 15, 2006 at 19:27 UTC | |
by Joost (Canon) on Nov 15, 2006 at 19:32 UTC | |
|
Re^2: sprintf and decimals (theory)
by tye (Sage) on Nov 15, 2006 at 23:16 UTC | |
|
Re^2: sprintf and decimals
by nosbod (Scribe) on Nov 15, 2006 at 18:46 UTC | |
by ikegami (Patriarch) on Nov 15, 2006 at 18:55 UTC |