Rounding


Rounding or rounding off is the process of adjusting a number to an approximate, more convenient value, often with a shorter or simpler representation. For example, replacing $23.4476 with $23.45, the fraction 312/937 with 1/3, or the expression √2 with 1.414.
Rounding is often done to obtain a value that is easier to report and communicate than the original. Rounding can also be important to avoid misleadingly precise reporting of a computed number, measurement, or estimate; for example, a quantity that was computed as 123456 but is known to be accurate only to within a few hundred units is usually better stated as "about 123500".
On the other hand, rounding of exact numbers will introduce some round-off error in the reported result. Rounding is almost unavoidable when reporting many computations – especially when dividing two numbers in integer or fixed-point 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 ill-conditioned 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 table-maker's dilemma".
Rounding has many similarities to the quantization that occurs when physical quantities must be encoded by numbers or digital signals.
A wavy equals sign is sometimes used to indicate rounding of exact numbers, e.g. 9.98 ≈ 10. This sign was introduced by Alfred George Greenhill in 1892.
Ideal characteristics of rounding methods include:
  1. Rounding should be done by a function. This way, when the same input is rounded in different instances, the output is unchanged.
  2. Calculations done with rounding should be close to those done without rounding.
  3. * As a result of and, the output from rounding should be close to its input, often as close as possible.
  4. To be considered rounding, the range will be a subset of the domain, often discrete. A classical range is the integers.
  5. Rounding should preserve symmetries that already exist between the domain and range. With finite precision, this translates to removing bias.
  6. A rounding method should have utility in computer science or human arithmetic where finite precision is used, and speed is a consideration.
Because it is not usually possible for a method to satisfy all ideal characteristics, many different rounding methods exist.
As a general rule, rounding is idempotent; i.e., once a number has been rounded, rounding it again to the same precision will not change its value. Rounding functions are also monotonic; i.e., rounding two numbers to the same absolute precision will not exchange their order. In the general case of a discrete range, they are piecewise constant functions.

Types of rounding

Typical rounding problems include:
Rounding problemExample inputResultRounding criterion
Approximating an irrational number by a fractionpi|22/71-digit-denominator
Approximating a rational number by a fraction with smaller denominator399 / 9413 / 71-digit-denominator
Approximating a fraction by a fractional decimal number5 / 31.66674 decimal places
Approximating a fractional decimal number by one with fewer digits2.17842.182 decimal places
Approximating a decimal integer by an integer with more trailing zeros23217232003 significant figures
Approximating a large decimal integer using scientific notation3009999993 significant figures
Approximating a value by a multiple of a specified amount48.245Multiple of 15
Approximating each of a finite set of real numbers by an integer so that the sum of the rounded numbers equals the rounded sum of the numbersSum of rounded elements equals rounded sum of elements

Rounding to integer

The most basic form of rounding is to replace an arbitrary number by an integer. All the following rounding modes are concrete implementations of an abstract single-argument "round" procedure. These are true functions.

Directed rounding to an integer

These four methods are called directed rounding to an integer, as the displacements from the original number to the rounded value are all directed toward or away from the same limiting value. Directed rounding is used in interval arithmetic and is often required in financial calculations.
If is positive, round-down is the same as round-toward-zero, and round-up is the same as round-away-from-zero. If is negative, round-down is the same as round-away-from-zero, and round-up is the same as round-toward-zero. In any case, if is an integer, is just.
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, although the market appeared to be rising. The problem was caused by the index being recalculated thousands of times daily, and always being truncated to 3 decimal places, in such a way that the rounding errors accumulated. Recalculating the index for the same period using rounding to the nearest thousandth rather than truncation corrected the index value from 524.811 up to 1098.892.
For the examples below, refers to the sign function applied to the original number,.

Rounding down

One may round down : is the largest integer that does not exceed.
For example, 23.7 gets rounded to 23, and −23.2 gets rounded to −24.

Rounding up

One may also round up : is the smallest integer that is not less than.
For example, 23.2 gets rounded to 24, and −23.7 gets rounded to −23.

Rounding toward zero

One may also round toward zero : is the integer that is closest to such that it is between 0 and ; i.e. is the integer part of, without its fraction digits.
For example, 23.7 gets rounded to 23, and −23.7 gets rounded to −23.

Rounding away from zero

One may also round away from zero : is the integer that is closest to 0 such that is between 0 and .
For example, 23.2 gets rounded to 24, and −23.2 gets rounded to −24.

Rounding to the nearest integer

These six methods are called rounding to the nearest integer. Rounding a number to the nearest integer requires some tie-breaking rule for those cases when is exactly half-way between two integers – that is, when the fraction part of is exactly 0.5.
If it were not for the 0.5 fractional parts, the round-off errors introduced by the round to nearest method would be symmetric: for every fraction that gets rounded down, there is a complementary fraction that gets rounded up by the same amount.
When rounding a large set of fixed-point numbers with uniformly distributed fractional parts, the rounding errors by all values, with the omission of those having 0.5 fractional part, would statistically compensate each other. This means that the expected value of the rounded numbers is equal to the expected value of the original numbers when numbers with fractional part 0.5 from the set are removed.
In practice, floating-point numbers are typically used, which have even more computational nuances because they are not equally spaced.

Rounding half up

One may round half up, a tie-breaking rule that is widely used in many disciplines. That is, half-way values of are always rounded up. If the fractional part of is exactly 0.5, then
For example, 23.5 gets rounded to 24, and −23.5 gets rounded to −23.
Some programming languages use "half up" to refer to round half away from zero rather than round half toward positive infinity.
This method only requires checking one digit to determine rounding direction in two's complement and similar representations.

Rounding half down

One may also round half down as opposed to the more common round half up. If the fractional part of is exactly 0.5, then
For example, 23.5 gets rounded to 23, and −23.5 gets rounded to −24.
Some programming languages use "half down" to refer to round half toward zero rather than round half toward negative infinity.

Rounding half toward zero

One may also round half toward zero as opposed to the conventional round half away from zero. If the fractional part of is exactly 0.5, then if is positive, and if is negative.
For example, 23.5 gets rounded to 23, and −23.5 gets rounded to −23.
This method treats positive and negative values symmetrically, and therefore is free of overall positive/negative bias if the original numbers are positive or negative with equal probability. It does, however, still have bias toward zero.

Rounding half away from zero

One may also round half away from zero, a tie-breaking rule that is commonly taught and used, namely: If the fractional part of is exactly 0.5, then if is positive, and if is negative.
For example, 23.5 gets rounded to 24, and −23.5 gets rounded to −24.
This can be more efficient on computers that use sign-magnitude representation for the values to be rounded, because only the first omitted digit needs to be considered to determine if it rounds up or down. This is one method used when rounding to significant figures due to its simplicity.
This method, also known as commercial rounding, treats positive and negative values symmetrically, and therefore is free of overall positive/negative bias if the original numbers are positive or negative with equal probability. It does, however, still have bias away from zero.
It is often used for currency conversions and price roundings as it is easy to explain by just considering the first fractional digit, independently of supplementary precision digits or sign of the amount.