Rounding a numerical value means replacing it by another value that is approximately equal but has a shorter, simpler, or more explicit representation; for example, replacing US$ 23.4476 by US$ 23.45, or the fraction 312/937 by 1/3, or by 1.41.
Rounding is often done on purpose to obtain a value that is easier to write and handle than the original. It may be done also to indicate the accuracy of a computed number; for example, a quantity that was computed as 123,456 but is known to be accurate only to within a couple of hundred units is better stated as about 123,500.
On the other hand, rounding introduces some roundoff error in the result. Rounding is almost unavoidable in many computations — especially when dividing two numbers in integer or fixedpoint arithmetic; when computing mathematical functions such as square roots, logarithms, and sines; or when using a floating point representation with a fixed number of significant digits. In a sequence of calculations, these rounding errors generally accumulate, and in certain illconditioned cases they may make the result meaningless.
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 tablemaker's dilemma".
Rounding has many similarities to the quantization that occurs when physical quantities must be encoded by numbers or digital signals.
Typical rounding problems are
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 (i.e., 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 magniture of the number to be rounded (or of the rounded result).
The increment m is normally a finite fraction in whatever number system that is used to represent the numbers. For display to humans, that usually means the decimal number system (that is, m is a integer times a power of 10, like 1/1000 or 25/100). For intermediate values stored in digital computers, it often means the binary number system (m is an integer times a power of 2).
The most basic form of rounding is to replace an arbitrary number by an integer.
There are many ways of rounding a number y to an integer q. The most common ones are
The last 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 the same limiting value (0, +∞, or ∞).
If y is positive, rounddown is the same as roundtowardszero, and roundup is the same as roundawayfromzero. If y is negative, rounddown is the same as roundawayfromzero, and roundup is the same as roundtowardszero. In any case, if y is integer, q is just y. The following table illustrates these rounding methods:
y  to nearest 
towards zero 
down  up  away from zero 
+23.67  +24  +23  +23  +24  +24 
+23.35  +23  +23  +23  +24  +24 
+23.00  +23  +23  +23  +23  +23 
−23.00  −23  −23  −23  −23  −23 
−23.35  −23  −23  −24  −23  −24 
−23.67  −24  −23  −24  −23  −24 
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.^{[1]}
Rounding a number y to the nearest integer requires some tiebreaking rule for those cases when y is exactly halfway between two integers — that is, when the fraction part of y is exactly .5.
The following tiebreaking rule, called round half up, is widely used in many disciplines:
That is, halfway 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.^{[citation needed]}
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 .732), there is a complementary fraction (namely, .268) 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" tiebreaking 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, roundtonearest with the roundhalfup rule is also known as asymmetric rounding.
The other tiebreaking method commonly taught and used is the round half away from zero, 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.
A tiebreaking 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. This variant of the roundtonearest method is also called unbiased rounding, convergent rounding, statistician's rounding, Dutch rounding, Gaussian rounding, or bankers' rounding. For most reasonable distributions of y values, the expected (average) value of the rounded numbers is essentially the same as that of the original numbers, even if the latter are all positive (or all negative). This is widely used in bookkeeping.
Another unbiased tiebreaking method is stochastic rounding:
Like roundhalftoeven, this rule is essentially free of overall bias; but it is also fair among even and odd q values. On the other hand, it introduces a random component into the result: performing the same computation twice on the same data may yield two different results.
One method, more obscure than most, is roundhalfalternatingly. If the fractional part is .5, alternate round up and round down. The first occurrence of a .5 fractional part, round up. The second occurrence, round down. So on so forth.
One may also round halfdown as opposed to the more common roundhalfup. The roundhalfup method is a common convention, but is nothing more than a convention.
In some contexts, all the rounding methods above may be unsatisfactory. For example, suppose that y is an accurate measurement of an audio signal, which is being rounded to an integer q in order to reduce the storage or transmission costs. If y changes slowly with time, any of the rounding method above will result in q being completely constant for long intervals, separated by sudden jumps of ±1. When the q signal is played back, these steps will be heard as a very disagreeable noise, and any variations of the original signal between two integer values will be completely lost.
One way to avoid this problem is to round each value y upwards with probability equal to its fraction, and round it downwards with the complement of that probability. For example, the number 23.17 would be rounded up to 24 with probability 0.17, and down to 23 with probability 10.17 = 0.83. (This is equivalent to rounding y+s to the nearest integer, where s is a random number uniformly distributed between 0 and 1.) With this special rounding, known as dithering, the sudden steps get replaced by a less objectionable noise, and even small variations in the original signal will be preserved to some extent. Like the stochastic approach to tiebreaking, dithering has no bias: if all fraction values are equally likely, rounding up by a certain amount is as likely as rounding down by that same amount; and the same is true for the sum of several rounded numbers. On the other hand, dithering introduces a random component in the result, much greater than that of stochastic tiebreaking.
More precisely, the roundoff error for each dithered number will be a uniformly distributed random variable with mean value zero and standard deviation and that of a sum of n rounded numbers will be a random variable with expected error zero and standard deviation .
In some contexts it is desirable to round a given number x to a "neat" fraction — that is, the nearest fraction z = m/n whose numerator m and denominator n do not exceed a given maximum. This problem is fairly distinct from that of rounding a value to a fixed number of decimal or binary digits, or to a multiple of a given unit m. This problem is related to Farey sequences, the SternBrocot tree, and continued fractions.
In floatingpoint arithmetic, rounding aims to turn a given value x into a value z with a specified number of significant digits. In other words, z should be a multiple of a number m that depends on the magnitude of z. The number m is a power of the base (usually 2 or 10) of the floatingpoint representation.
Apart from this detail, all the variants of rounding discussed above apply to the rounding of floatingpoint numbers as well.
For results where the rounded result would overflow the result for a directed rounding is either the appropriate infinity or the highest representable finite number depending on the direction. The result of an overflow for the usual case of round to even is always the appropriate infinity.
Rounding a number twice in succession to different precisions, with the latter precision being coarser, is not guaranteed to give the same result as rounding once to the final precision except in the case of directed rounding. For instance rounding 9.46 to one decimal gives 9.5, and then 10 when rounding to integer using rounding half to even, but would give 9 when rounded to integer directly.
Some computer languages and the IEEE 7542008 standard dictate that in straightforward calculations the result should not be rounded twice. This has been a particular problem with Java as it is designed to be run identically on different machines, special programming tricks have had to be used to achieve this with x87 floating point.^{[2]}^{[3]} The Java language was changed to allow different results where the difference does not matter and require a strictfp qualifier to be used when the results have to conform accurately.
It is possible to use rounded arithmetic to evaluate the exact value of a function with a discrete domain and range. For example, if we know that an integer n is a perfect square, we can compute its square root by converting n to a floatingpoint value x, computing the approximate square root y of x with floating point, and then rounding y to the nearest integer q. If n is not too big, the floatingpoint roundoff error in y will be less than 0.5, so the rounded value q will be the exact square root of n. In most modern computers, this method may be much faster than computing the square root of n by an allinteger algorithm.
William Kahan coined the term "The TableMaker's Dilemma" for the unknown cost of rounding transcendental functions:
"Nobody knows how much it would cost to compute y^w correctly rounded for every two floatingpoint arguments at which it does not over/underflow. Instead, reputable math libraries compute elementary transcendental functions mostly within slightly more than half an ulp and almost always well within one ulp. Why can't Y^W be rounded within half an ulp like SQRT? Because nobody knows how much computation it would cost... No general way exists to predict how many extra digits will have to be carried to compute a transcendental expression and round it correctly to some preassigned number of digits. Even the fact (if true) that a finite number of extra digits will ultimately suffice may be a deep theorem."^{[4]}
The IEEE floating point standard guarantees that add, subtract, multiply, divide, square root, and floating point remainder will give the correctly rounded result of the infinite precision operation. However, no such guarantee is given for more complex functions and they are typically only accurate to within the last bit at best.
Using the Gelfond–Schneider theorem and Lindemann–Weierstrass theorem many of the standard elementary functions can be proved to return transcendental results when given rational nonzero arguments; therefore it is always possible to correctly round such functions. However there is no way of determining in advance how accurate a result needs to be computed before a correctly rounded result can be produced.
There are some packages around now that offer full accuracy. The MPFR package gives correctly rounded arbitrary precision results. IBM have written a package for fast and accurate IEEE elementary functions and in the future the standard libraries may offer such precision.^{[5]}
It is possible to devise well defined computable numbers which it may never be possible to correctly round no matter how many digits are calculated. For instance if Goldbach's conjecture is true but unprovable then it is impossible to correctly round down 0.5 + 10^{n} where n is the first even number greater than 4 which is not the sum of two primes, or 0.5 if there is no such number. This can however be approximated to any given precision even if the conjecture is unprovable.
The concept of rounding is very old, perhaps older even than the concept of division. Some ancient clay tablets found in Mesopotamia contain tables with rounded values of reciprocals and square roots in base 60.^{[citation needed]}.
The Roundtoeven method has served as the ASTM (E29) standard since 1940. The origin of the terms unbiased rounding and statistician's rounding are fairly selfexplanatory. In the 1906 4th edition of Probability and Theory of Errors ^{[6]} Robert Simpson Woodward called this "the computer's rule" indicating that it was then in common use by human computers who calculated mathematical tables. Churchill Eisenhart's 1947 paper "Effects of Rounding or Grouping Data" (in Selected Techniques of Statistical Analysis, McGrawHill, 1947, Eisenhart, Hastay, and Wallis, editors) indicated that the practice was already "well established" in data analysis.
The origin of the term bankers' rounding remains more obscure. If this rounding method was ever a standard in banking, the evidence has proved extremely difficult to find. To the contrary, section 2 of the European Commission report The Introduction of the Euro and the Rounding of Currency Amounts ^{[7]} suggests that there had previously been no standard approach to rounding in banking; and it specifies that "halfway" amounts should be rounded up.
Until the 1980s, the rounding method used in floatingpoint computer arithmetic was usually fixed by the hardware, poorly documented, inconsistent, and different for each brand and model of computer. This situation changed after the IEEE 754 floating point standard was adopted by most computer manufacturers. The standard allows the user to choose among several rounding modes, and in each case specifies precisely how the results should be rounded. These features made numerical computations more predictable and machineindependent, and made possible the efficient and consistent implementation of interval arithmetic.
Most programming languages provide functions or special syntax to round fractional numbers in various ways. The earliest numeric languages, such as FORTRAN and C, would provide only one method, usually truncation (towards zero). This default method could be implied in certain contexts, such as when assigning a fractional number to an integer variable, or using a fractional number as an index of an array. Other kinds of rounding had to be programmed explicitly; for example, rounding a positive number to the nearest integer could be implemented by adding 0.5 and truncating.
In the last decades, however, the syntax and/or the standard libraries of most languages have commonly provided at least the four basic rounding functions (up, down, to nearest, and towards zero). The tiebreaking method may vary depending the language and version, and/or may be selectable by the programmer. Several languages follow the lead of the IEEE floatingpoint standard, and define these functions as taking a double precision float argument and returning the result of the same type, which then may be converted to an integer if necessary. Since the IEEE double precision format has 52 fraction bits, this approach may avoid spurious overflows in languages have 32bit integers. Some languages, such as PHP, provide functions that round a value to a specified number of decimal digits, e.g. from 4321.5678 to 4321.57 or 4300. On the other hand, truncation (round to zero) is still the default rounding method used by many languages, especially for the division of two integer values.
Some disciplines or institutions have issued standards or directives for rounding.
In a guideline issued in mid1966,^{[8]} the U.S. Office of the Federal Coordinator for Meteorology determined that weather data should be rounded to the nearest round number, with the "round half up" tiebreaking rule. For example, 1.5 rounded to integer should become 2, and −1.5 should become −1. Prior to that date, the tiebreaking rule was "round half away from zero".
Some meteorologists may write "−0" to indicate a temperature between 0.0 and −0.5 degrees (exclusive) that was rounded to integer. This notation is used when the negative sign is considered important, no matter how small is the magnitude; for example, when rounding temperatures in the Celsius scale, where below zero indicates freezing.^{[citation needed]}
