• Articles
  • Rounding Algorithms - Discussion
Published by
Apr 15, 2011

Rounding Algorithms - Discussion

Score: 3.4/5 (57 votes)
*****
On a slow but regular basis I get questions and commentary about the Article I posted a while ago:
http://www.cplusplus.com/forum/articles/3638
The following post exemplifies the general nature of them:
Name Withheld said:
I'm writing a bit of code to simulate an aspect of monetary policy for an economics course. While searching for the correct syntax/library for "ROUND" (obviously I'm not a programmer) I discovered your post dated Aug 18, 2008 at 8:56pm ( http://www.cplusplus.com/forum/articles/3638/#msg15562 ). In it you said,

The round half down did exactly what it should have. Given a number
exactly halfway between two integers (12.5) it rounds down to 12.

I don't believe this is correct, at least the way I learned it back in the 1900s. 10 divided by 2 might well be 5, but but it isn't halfway between the values linearly. The way you've done it seems to include zero as a value, but discounts it as a place holder. The objective of rounding is to decide whether to remain in the current magnitude or jump up to the next order of magnitude, yes?

Let's avoid enumeration for a moment by assuming ten dots (@) and denoting the midpoint (|) such that there are five dots on either side:

@  @  @  @  @  |  @  @  @  @  @

Now let's use integers. Here are the ten (and only ten) values/places of the decimal system with this same midpoint:

0  1  2  3  4  |  5  6  7  8  9

Unarguably the line is mid-way between zero and nine. If one were to assign a decimal value to that line, what would it be? 4.5? No, because the same problem exists, ad infinitum. In short, 4 or less is rounded down, 5 or more is rounded up.
Whether or not you believe a mathematical concept does not affect its truth.

Don't feel bad, though, because this particular point is confusing. And you have confused yourself more by mixing notations and value.

Given your ten dots:

@  @  @  @  @  |  @  @  @  @  @
let's count them:

1  2  3  4  5  |  6  7  8  9  10
That's right, there are exactly ten dots.
Exactly half of ten is five.

If you want to do it linearly, you can do that also, but you must remember to count properly:
 +--+--+--+--+--+--+--+--+--+--+
 0  1  2  3  4  5  6  7  8  9  10
 |              |              |
 fewest      the exact       most 
              center
Linearly, halfway between zero and ten dots (the fewest I have are zero, and the most I have are ten) is five. Since you are still unsure about that, go ahead and count how many spaces there are between each number. Notice that there are exactly ten.

Unarguably the line is mid-way between zero and nine. If one were to assign a decimal value to that line, what would it be? 4.5? No, because the same problem exists, ad infinitum.
You assume that because your premise is true that your conclusion is also true. Yes, the line is mid-way between zero and nine. But its correct decimal value is 4.5. You can see it on your graph, just by looking at it. The line is halfway between the 4 and the 5, which is 4 + 0.5 = 4.5.

In short, 4 or less is rounded down, 5 or more is rounded up.
Your assertation is at odds with the work of hundreds of thousands of competent, intelligent mathematicians throughout history and today. I think you ought to rethink your position.

As part of your rethinking, you may consider that you missed a large piece of the whole point in the article. There are more than one way to round numbers. The so-called "grade school" method, or round-half-up (the one you are favoring), is a biased method and is not always useful. (It is actually less useful than people think, since it assumes something about the data being processed.)

Finally, you are nit-picking me about a different rounding algorithm than the one you are considering. If you were making a comparison between the two, that would be one thing, but as you are not, then you are essentially complaining that the apple is not an orange. In the round-half-down algorithm, unlike the round-half-up algorithm, 0.5 or less rounds down and everything else rounds up.

I hope I have helped explain this better to you. Good luck!
Since Name Withheld posted when he went through elementary school I changed the date also. I was tempted to write 19x0, but I thought people would have just a little too much fun reading that as 2000 or 2011... (At least, I would have.)

And yes, the whole point in rounding is what kind of bias is appropriate for your data. The grade school method is biased toward positive infinity.

Not all biases are symmetric around zero. The grade school method is not, either.

Anyway, thanks!