Accurate rounding of transcendental mathematical functions is difficult, because the number of extra digits that need to be calculated to resolve whether to round up or down cannot be known in advance. This problem is known as "the table-maker's dilemma" ("below").
Rounding has many similarities to the quantization that occurs when physical quantities must be encoded by numbers or digital signals.
Typical rounding problems can include:
The most common type of rounding is to round to an integer; or, more generally, to an integer multiple of some increment—such as rounding to whole tenths of seconds, hundredths of a dollar, to whole multiples of 1/2 or 1/8 inch, to whole dozens or thousands, etc..
In general, rounding a number "x" to a multiple of some specified increment "m" entails the following steps:
For example, rounding "x" = 2.1784 dollars to whole cents (that is, to a multiple of 0.01) entails computing "y" = "x"/"m" = 2.1784/0.01 = 217.84, then rounding "y" to the integer "q" = 218, and finally computing "z" = "q"×"m" = 218×0.01 = 2.18.
When rounding to a predetermined number of significant digits, the increment "m" depends on the magnitude of the number to be rounded (or of the rounded result).
The increment "m" is normally a finite fraction in whatever numeral system that is used to represent the numbers. For display to humans, that usually means the decimal numeral system (that is, "m" is an integer times a power of 10, like 1/1000 or 25/100). For intermediate values stored in digital computers, it often means the binary numeral system ("m" is an integer times a power of 2).
The abstract single-argument "round()" function that returns an integer from an arbitrary real value has at least a dozen distinct concrete definitions presented in the rounding to integer section. The abstract two-argument "round()" function is formally defined here, but in many cases it is used with the implicit value "m" = 1 for the increment and then reduces to the equivalent abstract single-argument function, with also the same dozen distinct concrete definitions.
The most basic form of rounding is to replace an arbitrary number by an integer. All the following rounding modes are concrete implementations of the abstract single-argument "round()" function presented and used in the previous sections.
There are many ways of rounding a number "y" to an integer "q". The most common ones are
The first four methods are called directed rounding, as the displacements from the original number "y" to the rounded value "q" are all directed towards or away from the same limiting value (0, +∞, or −∞).
If "y" is positive, round-down is the same as round-towards-zero, and round-up is the same as round-away-from-zero. If "y" is negative, round-down is the same as round-away-from-zero, and round-up is the same as round-towards-zero. In any case, if "y" is integer, "q" is just "y". The following table illustrates these rounding methods:
Where many calculations are done in sequence, the choice of rounding method can have a very significant effect on the result. A famous instance involved a new index set up by the Vancouver Stock Exchange in 1982. It was initially set at 1000.000, and after 22 months had fallen to about 520 — whereas stock prices had generally increased in the period. The problem was caused by the index being recalculated thousands of times daily, and always being rounded down to 3 decimal places, in such a way that the rounding errors accumulated. Recalculating with better rounding gave an index value of 1098.892 at the end of the same period.
Rounding a number "y" to the nearest integer requires some tie-breaking rule for those cases when "y" is exactly half-way between two integers — that is, when the fraction part of "y" is exactly 0.5.
The following tie-breaking rule, called round half up (or round half towards plus infinity), is widely used in many disciplines. That is, half-way values "y" are always rounded up.
For example, by this rule the value 23.5 gets rounded to 24, but −23.5 gets rounded to −23.
This is one of two rules generally taught in US elementary mathematics classes.
If it were not for the 0.5 fractions, the roundoff errors introduced by the round to nearest method would be quite symmetric: for every fraction that gets rounded up (such as 0.268), there is a complementary fraction (namely, 0.732) that gets rounded down, by the same amount. When rounding a large set of numbers with random fractional parts, these rounding errors would statistically compensate each other, and the expected (average) value of the rounded numbers would be equal to the expected value of the original numbers.
However, the "round half up" tie-breaking rule is not symmetric, as the fractions that are exactly 0.5 always get rounded up. This asymmetry introduces a positive bias in the roundoff errors. For example, if the fraction of "y" consists of three random decimal digits, then the expected value of "q" will be 0.0005 higher than the expected value of "y". For this reason, round-to-nearest with the "round half up" rule is also (ambiguously) known as asymmetric rounding.
One reason for rounding up at 0.5 is that only one digit need be examined. When seeing 17.50000..., for example, the first three figures, 17.5, determines that the figure would be rounded up to 18. If the opposite rule were used (round half down), then all the zero decimal places would need to be examined to determine if the value is exactly 17.5.
One may also use round half down (or round half towards minus infinity) as opposed to the more common "round half up" (the "round half up" method is a common convention, but is nothing more than a convention).
For example, 23.5 gets rounded to 23, and −23.5 gets rounded to −24.
The "round half down" tie-breaking rule is not symmetric, as the fractions that are exactly 0.5 always get rounded down. This asymmetry introduces a negative bias in the roundoff errors. For example, if the fraction of "y" consists of three random decimal digits, then the expected value of "q" will be 0.0005 lower than the expected value of "y". For this reason, round-to-nearest with the "round half down" rule is also (ambiguously) known as asymmetric rounding.
The other tie-breaking method commonly taught and used is the round half away from zero (or round half towards infinity), namely:
For example, 23.5 gets rounded to 24, and −23.5 gets rounded to −24.
This method treats positive and negative values symmetrically, and therefore is free of overall bias if the original numbers are positive or negative with equal probability. However, this rule will still introduce a positive bias for positive numbers, and a negative bias for the negative ones.
It is often used for currency conversions and price roundings (when the amount is first converted into the smallest significant subdivision of the currency, such as cents of a euro) as it is easy to explain by just considering the first fractional digit, independently of supplementary precision digits or sign of the amount (for strict equivalence between the paying and recipient of the amount).
One may also round half towards zero (or round half away from infinity) as opposed to the more common "round half away from zero" (the "round half away from zero" method is a common convention, but is nothing more than a convention).
For example, 23.5 gets rounded to 23, and −23.5 gets rounded to −23.
This method also treats positive and negative values symmetrically, and therefore is free of overall bias if the original numbers are positive or negative with equal probability. However, this rule will still introduce a negative bias for positive numbers, and a positive bias for the negative ones.
A tie-breaking rule that is even less biased is round half to even, namely
Thus, for example, +23.5 becomes +24, +22.5 becomes +22, −22.5 becomes −22, and −23.5 becomes −24.