maths.free › Arithmetic › Numbers › Rounding
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.
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.
- Rounding should be done by a function. This way, when the same input is rounded in different instances, the output is unchanged.
- Calculations done with rounding should be close to those done without rounding.
- As a result of (1) and (2), the output from rounding should be close to its input, often as close as possible.
- To be considered rounding, the range will be a subset of the domain, often discrete. A classical range is the integers.
- Rounding should preserve symmetries that already exist between the domain and range. With finite precision (or a discrete domain), this translates to removing bias.
- A rounding method should have utility in computer science or human arithmetic where finite precision is used, and speed is a consideration.
Condensed: the full section is in Wikipedia.
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 (with the exception of those that use randomness).
Directed rounding to an integer
These four methods are called directed rounding to an integer, as the displacements from the original number x to the rounded value y are all directed toward or away from the same limiting value (0, +∞, or −∞). Directed rounding is used in interval arithmetic and is often required in financial calculations.
If x is positive, round-down is the same as round-toward-zero, and round-up is the same as round-away-from-zero. If x 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 x is an integer, y is just x.
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 (three decimal places of accuracy), 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 (rounded down) 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, sgn(x) refers to the sign function applied to the original number, x.
Rounding to the nearest integer
These six methods are called rounding to the nearest integer. Rounding a number x to the nearest integer requires some tie-breaking rule for those cases when x is exactly half-way between two integers, that is, when the fraction part of x 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 (such as 0.268), there is a complementary fraction (namely, 0.732) 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 (average) 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 to a specified multiple
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 positive value m entails the following steps:
\(\mathrm{roundToMultiple}(x, m) = \mathrm{round}(x/m) \times m\)
For example, rounding x = 2.1784 dollars to whole cents (i.e., to a multiple of 0.01) entails computing 2.1784 / 0.01 = 217.84, then rounding that to 218, and finally computing 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 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 "roundToMultiple()" 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.
Floating-point rounding
In floating-point arithmetic, rounding aims to turn a given value x into a value y with a specified number of significant digits. In other words, y should be a multiple of a number m that depends on the magnitude of x. The number m is a power of the base (usually 2 or 10) of the floating-point representation.
Apart from this detail, all the variants of rounding discussed above apply to the rounding of floating-point numbers as well. The algorithm for such rounding is presented in the Scaled rounding section above, but with a constant scaling factor s = 1, and an integer base b > 1.
Where the rounded result would overflow the result for a directed rounding is either the appropriate signed infinity when "rounding away from zero", or the highest representable positive finite number (or the lowest representable negative finite number if x is negative), when "rounding toward zero". The result of an overflow for the usual case of round to nearest is always the appropriate infinity.
Rounding to a simple fraction
In some contexts it is desirable to round a given number x to a "neat" fraction, that is, the nearest fraction y = 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 Stern-Brocot tree, and continued fractions.
Rounding to an available value
Finished lumber, writing paper, electronic components, and many other products are usually sold in only a few standard values.
Many design procedures describe how to calculate an approximate value, and then "round" to some standard size using phrases such as "round down to nearest standard value", "round up to nearest standard value", or "round to nearest standard value".
When a set of preferred values is equally spaced on a logarithmic scale, choosing the closest preferred value to any given value can be seen as a form of scaled rounding. Such rounded values can be directly calculated.
Arbitrary bins
More general rounding rules can separate values at arbitrary break points, used for example in data binning. A related mathematically formalized tool is signpost sequences, which use notions of distance other than the simple difference: for example, a sequence may round to the integer with the smallest relative (percent) error.
Dithering and error diffusion
When digitizing continuous signals, such as sound waves, the overall effect of a number of measurements is more important than the accuracy of each individual measurement. In these circumstances, dithering, and a related technique, error diffusion, are normally used. A related technique called pulse-width modulation is used to achieve analog type output from an inertial device by rapidly pulsing the power with a variable duty cycle. Delta-sigma modulation is commonly used for converting between real-world signals and digital signals, which allows control of the frequency statistics of quantization.
Error diffusion tries to ensure the error, on average, is minimized. When dealing with a gentle slope from one to zero, the output would be zero for the first few terms until the sum of the error and the current value becomes greater than 0.5, in which case a 1 is output and the difference subtracted from the error so far. Floyd-Steinberg dithering is a popular error diffusion procedure when digitizing images.
As a one-dimensional example, suppose the numbers 0.9677, 0.9204, 0.7451, and 0.3091 occur in order and each is to be rounded to a multiple of 0.01. In this case the cumulative sums, 0.9677, 1.8881 = 0.9677 + 0.9204, 2.6332 = 0.9677 + 0.9204 + 0.7451, and 2.9423 = 0.9677 + 0.9204 + 0.7451 + 0.3091, are each rounded to a multiple of 0.01: 0.97, 1.89, 2.63, and 2.94. The first of these and the differences of adjacent values give the desired rounded values: 0.97, 0.92 = 1.89 − 0.97, 0.74 = 2.63 − 1.89, and 0.31 = 2.94 − 2.63.
Monte Carlo arithmetic
Monte Carlo arithmetic is a technique in Monte Carlo methods where the rounding is randomly up or down. Stochastic rounding can be used for Monte Carlo arithmetic, but in general, just rounding up or down with equal probability is more often used. Repeated runs will give a random distribution of results which can indicate the stability of the computation.
Exact computation with rounded arithmetic
It is possible to use rounded arithmetic to evaluate the exact value of a function with integer domain and range. For example, if an integer n is known to be a perfect square, its square root can be computed by converting n to a floating-point value z, computing the approximate square root x of z with floating point, and then rounding x to the nearest integer y. If n is not too big, the floating-point round-off error in x will be less than 0.5, so the rounded value y will be the exact square root of n. This is essentially why slide rules could be used for exact arithmetic.
Double rounding
Rounding a number twice in succession to different levels of precision, 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 the nearest tenth gives 9.5, and then 10 when rounding to the nearest integer using rounding half to even, but would give 9 when rounded directly using the same method. Borman and Chatfield discuss the implications of double rounding when comparing data rounded to one decimal place to specification limits expressed using integers.
In Martinez v. Allstate and Sendejo v. Farmers, litigated between 1995 and 1997, the insurance companies argued that double rounding premiums was permissible and in fact required. The US courts ruled against the insurance companies and ordered them to adopt rules to ensure single rounding.
Some computer languages and the IEEE 754-2008 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. 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; strict floating point was restored in Java 17.
In some algorithms, an intermediate result is computed in a larger precision, then must be rounded to the final precision. Double rounding can be avoided by choosing an adequate rounding for the intermediate computation. This consists in avoiding to round to midpoints for the final rounding (except when the midpoint is exact). In binary arithmetic, the idea is to round the result toward zero, and set the least significant bit to 1 if the rounded result is inexact; this rounding is called sticky rounding. Equivalently, it consists in returning the intermediate result when it is exactly representable, and the nearest floating-point number with an odd significand otherwise; this is why it is also known as rounding to odd. A concrete implementation of this approach, for binary and decimal arithmetic, is implemented as Rounding to prepare for shorter precision.
Rounding to prepare for shorter precision
This rounding mode is used to avoid getting a potentially wrong result after multiple roundings. This can be achieved if all roundings except the final one are done using rounding to prepare for shorter precision ("RPSP"), and only the final rounding uses the externally requested mode.
With decimal arithmetic, final digits of 0 and 5 are avoided when the input is not representable exactly; if there is a choice between numbers with the least significant digit 0 or 1, 4 or 5, 5 or 6, 9 or 0, then the digit different from 0 or 5 shall be selected; otherwise, the choice is arbitrary. IBM defines that, in the latter case, a digit with the smaller magnitude shall be selected. RPSP can be applied with the step between two consequent roundings as small as a single digit (for example, rounding to 1/10 can be applied after rounding to 1/100). For example, when rounding to integer,
- 20.0 is rounded to 20;
- 20.01, 20.1, 20.9, 20.99, 21, 21.01, 21.9, 21.99 are rounded to 21 (avoiding a final 0);
- 22.0, 22.1, 22.9, 22.99 are rounded to 22;
- 24.0, 24.1, 24.9, 24.99 are rounded to 24 (avoiding a final 5);
- 25.0 is rounded to 25;
- 25.01, 25.1 are rounded to 26 (avoiding a final 5).
In the example from "Double rounding" section, rounding 9.46 to one decimal gives 9.4, which rounding to integer in turn gives 9.
With binary arithmetic, this rounding is also called "round to odd" (not to be confused with "round half to odd"). For example, when rounding to 1/4 (0.01 in binary),
- x = 2.0 ⇒ result is 2 (10.00 in binary)
- 2.0 < x < 2.5 ⇒ result is 2.25 (10.01 in binary)
- x = 2.5 ⇒ result is 2.5 (10.10 in binary)
- 2.5 < x < 3.0 ⇒ result is 2.75 (10.11 in binary)
- x = 3.0 ⇒ result is 3 (11.00 in binary)
For correct results with binary arithmetic, each rounding step must remove at least 2 binary digits, otherwise, wrong results may appear. For example,
- 3.125 RPSP to 1/4 ⇒ result is 3.25
- 3.25 RPSP to 1/2 ⇒ result is 3.5
- 3.5 round-half-to-even to 1 ⇒ result is 4 (wrong)
If the erroneous middle step is removed, the final rounding to integer rounds 3.25 to the correct value of 3.
RPSP is implemented in hardware in IBM zSeries and pSeries. In Python module "Decimal", Tcl module "math", Haskell package "decimal-arithmetic", and possibly others, this mode is called ROUND_05UP or round05up.
עכשיו אתה אין מחשבון שמסדר את זה, אבל החלקים שלו ניתנים לחישוב. נסה אחד מתחת, או הקלד את שלך.
סמלים המשמשים כאן
הקש כל סמל להגדרה המלאה, תמונה, ומה כל אות בזה אומר.
שאלות שאנשים שואלים
Why does the order of operations matter?
Because 2 + 3 × 4 would otherwise be two different numbers. The convention (brackets, exponents, multiplication and division, addition and subtraction) exists so every reader gets the same value from the same expression.
How do I check an arithmetic answer?
Estimate first (round every number and compute roughly), then compare. If the estimate and the exact answer disagree by more than a little, one of them is wrong. The solver shows every operation, so you can find which line went astray.
Why are fractions harder than decimals?
They are not harder, they are more exact: 1/3 is a precise number, 0.333 is an approximation. Fractions need a common denominator to add, which is the one extra step people trip on.
חלקים מהדף הזה מותאמים Wikipedia (CC BY-SA 4.0). מעוכלים ומסבירים מחדש כאן; טעויות הן שלנו.