Rounding Algorithms 279
dtmos writes "Clive Maxfield has an interesting article up on PL DesignLine cataloging most (all?) of the known rounding algorithms used in computer math. As he states, "...the mind soon boggles at the variety and intricacies of the rounding algorithms that may be used for different applications ... round-up, round-down, round-toward-nearest, arithmetic rounding, round-half-up, round-half-down, round-half-even, round-half-odd, round-toward-zero, round-away-from-zero, round-ceiling, round-floor, truncation (chopping), round-alternate, and round-random (stochastic rounding), to name but a few." It's a good read, especially if you *think* you know what your programs are doing."
Re:Most important... (Score:5, Informative)
It keeps adding the slope value for every x increment and when it overloads it also makes the y position go up one.
Or something like that. Bresenham's I believe.
To get on topic I would use the usual "(x).5 to (x+1).499~9 goes to (x+1)" way.
For negative, just ignore the sign when doing it, e.g. -1.6 -> -2
Re:Most important... (Score:2, Informative)
Re:I read the first half of the article... (Score:2, Informative)
IEEE Standard (Score:4, Informative)
2.5 = 2
3.5 = 4
Re:Seems dumb to me (Score:2, Informative)
Re:I read the first half of the article... (Score:3, Informative)
floor() is slow (Score:1, Informative)
typedef union masker
{
double d;
int64_t n;
} masker_t;
int round( double x )
{
static const int64_t mask = 1LL 63;
masker_t half;
half.d = 0.5;
masker_t a;
a.d = x;
half.n |= ( a.n & mask );
x += half.d;
return( ( int ) x );
}
Precision (Score:4, Informative)
I would say that $3.00 is just as precise as $3.21. If you want less precision, you have to go to $3...
Only with money in fractions (Score:5, Informative)
For pretty much all other cases it is broken, wrong, bad, very bad, and misguided. It is a kludge cut from the same cloth as using red and black ink, parenthesis, or location on the page (and all the permutations thereof) to indicate the sign of a number. Do not try to do any sort of scientific calculations, or engineering, or anything else that matters and round in this way.
Why? Because contrary to what some people think, there is no systematic bias in always rounding up. There are exactly as many values that will be rounded down as will be rounded up if you always round exact halves up. I think the trap that people fall into is forgetting that x.000... rounds down (they think of it as somehow "not rounding").
--MarkusQ
Round Toward Mean? (Score:4, Informative)
It's useful for smoothing out images if you use this for each color channel (RGB, CMYK, HSV, etc.).
Re:I read the first half of the article... (Score:3, Informative)
This one too: What Every Computer Scientist Should Know About Floating-Point Arithmetic [sun.com].
Re:Precision (Score:3, Informative)
Re:Only with money in fractions (Score:4, Informative)
I think you might be mistaken. Round to the nearest even is statisticly significantly more accurate. Rounding halves up does nothing for accuracy as you seem to imply. Large data sets of any type of data will be biased if rounding halves up, whereas rounding to the nearest even is ever more accurate with each datapoint. Your statement about rounding to even being bad makes me think you haven't fully grasped the underlying concept, I've never seen rounding halves up used for anything in a major environment simply because it is almost always the wrong thing to use.
On the contrary, I understand and have worked with this sort of thing for years. I know whereof I speak, and the situation is exactly opposite of what you claim. Specifically:
Note that this is basically your list, with the claims reversed. So we disagree totally. Now let me explain why I am correct. First, let's review what you do when you round to the nearest integer (without loss of generality; rounding to the nearest 1/10th, or even 1/137th, is isomorphic).
Note that the only difference in results between these rules comes from numbers where:
For example, the number 4.56106531 would be rounded to 4 in the "nearest even" case or to 5 in the "round up" case But clearly, the "nearest even" result is less accurate, and introduces a significant bias. 4.56106531 is closer to 5 than to 4, and should be rounded up. Always.
At this point, you may object that you aren't planning on truncating before you apply the rule (or, equivalently, you only do the even odd dance on "exact" halves). But how did you get an "exact" half? Unless you have infinite precision floating point hardware, less significant bits fell off the bottom of your number; unless they were all zero your "exact half" is the result of truncation and the above logic still applies.
The only common case where it doesn't apply is (as I stated originally) when dealing with money, where 1) your sample is biased to contain "exact halves" and 2) it is more important to be "fair" than it is to be accurate. This, in any case, is more of a convention than a fact of mathematics; we agree that money is tracted to a certain point and ignore the half pennies owed and the $0.00004537531 of interest due on them; if we didn't even money would not be an exception to the logic above.
-- MarkusQ
Re:Only with money in fractions (Score:3, Informative)
Your example proves my point. Which distribution is more arbitrary; "Arithmetically uniform," or the set "{0.2, 0.3, and 0.5}"?
To be sure, if the distribution is, for example, geometrically uniform (i.e., uniformly distributed exponents), then Benford's Law [wikipedia.org] applies, and significantly more values in the range [0..10] (say) will have fractions less than 0.5. This effect decreases dramatically for large values, since the fractional component (which is what we care about) shifts to less and less significant bits. But If you find yourself rounding lots of very small values to integer, then your loss of precision will far exceed the error caused by your 0.5-rounding choice, and you should probably rethink what you're doing.
In typical real-life scenarios, it's logical to assume that your set of numbers will have a reasonably smooth distribution (arithmetically uniform or not), and also that most values will be considerably larger than the rounding granularity. (Dollar values in the $10-$100 range rounded to pennies, for example.) Any such distribution will approach arithmetic uniformity at the scale of the rounding granularity (pennies). Hence, the choice of an arithmetically uniform distribution between successive integers is NOT arbitrary, and accurately represents most real-life distributions. For such distributions, the 0.5->1 rounding convention introduces a small but measurable positive bias.
Re:Most important... (Score:4, Informative)
Re:Only with money in fractions (Score:3, Informative)