Rounding a number to nearest even
Mikael Olofsson
mikael at isy.liu.se
Fri Apr 11 11:19:50 EDT 2008
cokofreedom at gmail.com commented about rounding towards even numbers
from mid-way between integers as opposed to for instance always rounding
up in those cases:
> Strange request though, why do you need it that way, because 2.5 is
> CLOSER to 3 than to 2...
That's exactly how I was taught to do rounding in what-ever low-level
class it was. The idea is to avoid a bias, which assumes that the
original values are already quantized. Assume that we have values
quantized to one decimal only, and assume that all values of this
decimal are equally likely. Also assume that the integer part of our
numbers are equally likely to be even or odd. Then the average rounding
error when rounding to integers will be 0.05 if you always round up when
the decimal is 5. If you round towards an even number instead when the
decimal is 5, then you will round up half of those times, and round down
the other half, and the average rounding error will be 0. That's the
idea. Of course you could argue that it would be even more fair to make
the choice based on the tossing of a fair coin.
Note that if you do not have quantized values and assuming that the
fraction part is evenly distributed between 0 and 1, than this whole
argument is moot. The probability of getting exactly 0.5 is zero in that
case, just as the probability of getting any other specified number is zero.
That said, measurements are in practice always quantized, and rounding
towards an even number when mid-way between avoids an average error of
half the original precision.
As a side-note: The the smallest coin in Swedish currency SEK is 0.50,
but prices in stores are given with two decimals, i.e. with precision
0.01. But what if your goods add up to 12.34? The standard in Swedish
stores, after adding the prices of your goods, is to round the number to
the nearest whole or half SEK, which means that decimals 25 and 75 are
mid-way between. In those cases the rounding is usually done to the
nearest whole SEK, which is based on precicely the above reasoning. If
they did not do that, I could argue that they are stealing on average
0.005 SEK from me every time I go to the store. Well... I could live
with that, since 0.005 SEK is a ridiculously small amount, and even if I
make thousands of such transactions per year, it still sums up to a
neglectable amount.
Another side-note: My 12-year old son is now being taught to always
round up from mid-way between. Yet another example of the degradation of
maths in schools.
/MiO
More information about the Python-list
mailing list