Rounding
From Wikipedia, the free encyclopedia
- For lip-rounding in phonetics, see Labialisation and Roundedness. For other uses, see Rounding (disambiguation).
This article needs additional citations for verification. Please help improve this article by adding reliable references (ideally, using inline citations). Unsourced material may be challenged and removed. (November 2008) |
Rounding involves reducing the number of significant digits in a number. The result of rounding is a "shorter" number having fewer non-zero digits yet similar in magnitude. The result is less precise but easier to use.
For example: 73 rounded to the nearest ten is 70, because 73 is closer to 70 than to 80.
Researchers may analyze rounding as a form of quantization.
People follow many different rules and procedures when rounding. Some of the more popular appear below.
Contents |
[edit] Common method
This section is in need of attention from an expert on the subject. Please help recruit one or improve this article yourself. See the talk page for details. Please consider using {{Expert-subject}} to associate this request with a WikiProject. (February 2009) |
This method is commonly used in mathematical applications, for example in accounting. It is the one generally taught in elementary mathematics classes.[citation needed] This method is also known as Asymmetric Arithmetic Rounding or Round-Half-Up (Asymmetric Implementation)
1. Divide it by the unit to which it is to be rounded
2. Round it to the nearest whole number, unless it ends in exactly .5
3. If it ends in exactly .5, then add 0.5
4. Multiply it by the unit to which it is to be rounded
Examples (rounded to hundredths):
- 3.0448 → 304.48 → 304 → 3.04
- 3.0450 → 304.50 → 305 → 3.05
- 3.0452 → 304.52 → 305 → 3.05
- -3.0448 → -304.48 → -304 → -3.04
- -3.0450 → -304.50 → -304 → -3.04
- -3.0452 → -304.52 → -305 → -3.05
As the name implies, this can introduce a bias: if all the unrounded numbers had four decimal places, say, then in our example the expected average of the rounded numbers will be 0.0005 higher than that of the unrounded numbers.
[edit] Round-to-even method
This method, also known as unbiased rounding, convergent rounding, statistician's rounding, Dutch rounding, Gaussian rounding, or bankers' rounding, exactly replicates the common method of rounding except when the digit(s) following the rounding digit starts with a five and has no non-zero digits after it.
Despite the custom of rounding the number 4.5 up to 5, in fact 4.5 is no nearer to 5 than it is to 4 (it is 0.5 away from both). When dealing with large sets of scientific or statistical data, where trends are important, traditional rounding on average biases the data upwards slightly. Over a large set of data, or when many subsequent rounding operations are performed as in digital signal processing, the round-to-even rule tends to reduce the total rounding error, with (on average) an equal portion of numbers rounding up as rounding down. This generally reduces upwards skewing of the result.
The new algorithm becomes (only rule 3. changes):
1. Divide it by the unit to which it is to be rounded
2. Round it to the nearest whole number, unless it ends in exactly .5
3. If it ends in exactly .5, then round towards the nearest even whole number
4. Multiply it by the unit to which it is to be rounded
Examples (rounded to hundredths):
- 3.0448 → 304.48 → 304 → 3.04
- 3.0450 → 304.50 → 304 → 3.04
- 3.0452 → 304.52 → 305 → 3.05
- -3.0448 → -304.48 → -304 → -3.04
- -3.0450 → -304.50 → -304 → -3.04
- -3.0452 → -304.52 → -305 → -3.05
Thus the bias mentioned above has been removed. Provided the distribution of the unrounded numbers is uniform, the expected average of the rounded numbers will be the same as that of the unrounded numbers.
[edit] History
The Round-to-even method has served as the ASTM (E-29) standard since 1940. The origin of the terms unbiased rounding and statistician's rounding are fairly self-explanatory. In the 1906 4th edition of Probability and Theory of Errors [1] 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 [2] suggests that there had previously been no standard approach to rounding in banking, and specifies that "half-way" amounts should be rounded up.
[edit] Other methods of rounding
Other methods of rounding exist, but operate mostly in computers and calculators, statistics and science. In computers and calculators, these methods are used for one of two reasons: speed of computation or usefulness in certain computer algorithms. In statistics and science, the primary use of alternate rounding schemes is to reduce bias, rounding error and drift—these are similar to round-to-even rounding. They make a statistical or scientific calculation more accurate.
[edit] Ease of computation
Other methods of rounding include "round towards zero" (also known as truncation) and "round away from zero". These introduce more round-off error and therefore statistics and science tend to avoid them; they are still used in computer algorithms because they are slightly easier and faster to compute. Two specialized methods used in mathematics and computer science are the floor (always round down to the nearest integer) and ceiling (always round up to the nearest integer).
[edit] Statistical accuracy
Stochastic rounding rounds to the nearest integer, but when the two integers are equidistant (e.g., 3.5), then it is rounded up with probability 0.5 and down with probability 0.5. This reduces any drift, but adds randomness to the process. Thus, if you perform a calculation with stochastic rounding twice, you may not end up with the same answer. The motivation is similar to statistician's rounding.
Under some circumstances (for example, estimating integer counts derived from a probability distribution function), the best statistical accuracy may be obtained by rounding based on a probability that is proportional to the fractional part of the number. For example, the number 3.1 would be rounded to 3 with probability 0.9, or to 4 with probability 0.1. In general, given a positive number of the form I.F (where I is the integer part and F is the fractional part), one would round to integer I with probability 1-F, or to integer I+1 with probability F.
[edit] Rounding in an exact computation
People often use rounding to get a number that is easier to use, at the cost of making it less precise. However, for evaluating a function with a discrete domain and range, rounding may be involved in an exact computation, e.g. to find the number of Sundays between two dates, or to compute a Fibonacci number. In such cases the algorithm can typically be set up such that computational rounding errors before the explicit rounding do not affect the outcome of the latter. For example, if an integer divided by 7 is rounded to an integer, a computational rounding error up to 1/14 in the division (which is much more than is possible in typical cases) does not affect the outcome. (It is important that the dividend is an integer and the divisor is an odd integer: small errors can affect the outcome of rounding 1.5/3 or 3/2 to an integer.)
[edit] Rounding functions in programming languages
- C
- C99 specifies (in
<math.h>
):round()
: round to nearest integer, halfway away from zerorint()
,nearbyint()
: round according to current floating-point rounding directionceil()
: smallest integral value not less than argument (round up)floor()
: largest integral value not greater than argument (round down)trunc()
,(int)
[1]: round towards zero
- The current floating-point rounding direction may, depending on implementation, be retrieved and set using the
fegetround()
/fesetround()
functions defined in<fenv.h>
; the available directions are specified to be at least those in IEEE 854 (see IEEE 754#Rounding floating-point numbers) which include round-to-even, round-down, round-up, and round-to-zero.
- C99 specifies (in
- Fortran:
NINT
: round to nearest integer, return integer resultANINT
: round to nearest integer, return real resultINT
: truncate (round towards zero), return integer resultAINT
: truncate (round towards zero), return real resultCEILING
: smallest integral value not less than argument (round up) (Fortran-90)FLOOR
: largest integral value not greater than argument (round down) (Fortran-90)
- Java
Math
andStrictMath
: round-half-up (asymmetric) or round-to-even (Math.rint).[2][3]BigDecimal
: Supports "round up", "round down", "round floor", "round ceiling", "round half up", "round half down", "round half even" and even a "rounding unnecessary" mode. Note, however, that the implementation of round half-up is symmetric, unlike the implementation of round half-up in Java.Math.Math.round(-3.5)
orStrictMath.round(-3.5)
gives −3.
- JavaScript:
- Uses Asymmetric Arithmetic Rounding
Math.round(-3.5)
gives −4.
- Common Lisp:
- Uses round-to-even
(round -3.5)
gives −4.
- Pascal:
Round
andRoundTo
(nonstandard) use bankers' roundingStr
(nonstandard),Write
andFloatToStr
(nonstandard) don't use bankers' roundingCeil()
: smallest integral value not less than argument (round up)Floor()
: largest integral value not greater than argument (round down)Trunc()
: round towards zero
- Perl:
printf "%f"
orsprintf "%f"
: use bankers' roundingint
: chops off fractional part, rounding towards zeroPOSIX::ceil
: smallest integral value not less than argument (round up)POSIX::floor
: largest integral value not greater than argument (round down)
- PHP:
round(-3.5)
gives −4.round(8.7352, 3)
gives 8.735.round(4278.5, -2)
gives 4300.
- Python:
- Versions 2.5.x and prior use common rounding for POSITIVE numbers:
round(0.5)
is 1.0. - Note that negative integer division is not rounded towards zero but always rounded down: 7/3=2, −7/3=−3.
- Version 3.0 and transitional versions 2.6.x
round()
function uses round-to-even. - Version 3.0
//
operator rounds down towards -inf, similar to previous versions. - As of version 2.4, Python includes class Decimal in module decimal.
- Class Decimal provides exact numerical representation and several rounding modes.
- Versions 2.5.x and prior use common rounding for POSITIVE numbers:
- SQL:
- Visual Basic for Applications:
- Uses Round-Half-Even (Bankers' Rounding)
? Round(2.5, 0)
gives 2.- http://support.microsoft.com/kb/194983
- Microsoft SQL Server:
- Uses either Symmetric Arithmetic Rounding or Symmetric Round Down (Fix) depending on arguments
- SELECT Round(2.5, 0) gives 3.
- Microsoft Excel:
- Uses Symmetric Arithmetic Rounding
= ROUND(2.5, 0)
gives 3.= ROUND(3.5, 0)
gives 4.= ROUND(-3.5, 0)
gives −4
- Microsoft .NET Framework
- Uses Round-Half-Even by default. Since v2 of the framework Symmetric Arithmetic Rounding is also available and the caller can specify which method to use.[6]
- C#
- C# programs generally use methods of the .NET framework for rounding. (See above.)
- Visual Basic .NET
CType(value, Integer)
uses bankers' rounding. Since this is a .NET language, programs may also use any of the rounding methods of the .NET framework (see above).
- XSLT 1.0:
- Uses round half even
- AppleScript
round
is the rounding function. By default, it rounds to the nearest even number. However, it takes an optional tag that specifies how to round:rounding up
,rounding down
,rounding toward zero
, orrounding as taught in school
(round-half-up).rounding to nearest
is a synonym for the default behavior.
[edit] Rounding in meteorology
[edit] U.S. Weather Observations
According to the Office of the Federal Coordinator for Meteorology, with the exception of pressure,
If the fractional part of a positive number to be dropped is equal to or greater than one-half, the preceding digit shall be increased by one. If the fractional part of a negative number to be dropped is greater than one-half, the preceding digit shall be decreased by one. In all other cases, the preceding digit shall remain unchanged. For example, 1.5 becomes 2, −1.5 becomes −1, 1.3 becomes 1, and −2.6 becomes −3.[7]
This guideline has been in effect since mid-1996. Prior to that date, if the fraction equal to one half, the value was rounded away from zero. For example, a −1.5 became a −2, and a +1.5 became a +2.
[edit] Negative zero in meteorology
Meteorologists may round temperatures between 0.0 and −0.5 degrees (exclusive) to −0 to indicate a temperature which is below zero, but not cold enough to be rounded to −1 or less. It is used especially in the Celsius scale, where below zero indicates freezing. It may be used, for example, to allow tallying of below-zero days.
[edit] See also
[edit] External links
- An introduction to different rounding algorithms that is accessible to a general audience but especially useful to those studying computer science and electronics.
- A complete treatment of mathematical rounding. by John Kennedy
- How To Implement Custom Rounding Procedures by Microsoft
[edit] References
- ^ typecasting to an int returns an int instead of a double unlike the other functions.
- ^ Math 1.5.0
java.lang.Math
Java 2 Platform SE 5.0. Sun Microsystems (2005-07-22). Retrieved on 2008-11-08. - ^ StrictMath 1.5.0
java.lang.StrictMath
Java 2 Platform SE 5.0. Sun Microsystems (2007-01-03). Retrieved on 2008-11-08. - ^ Jones, Arie; Ryan K. Stephens, Ronald R. Plew, Alex Kriegel, Robert F. Garrett (2005). SQL Functions Programmer's Reference. Indianapolis, IN: Wiley. pp. 127. ISBN 9780764569012.
- ^ Jones, Arie; Ryan K. Stephens, Ronald R. Plew, Alex Kriegel, Robert F. Garrett (2005). SQL Functions Programmer's Reference. Indianapolis, IN: Wiley. pp. 127. ISBN 9780764569012.
- ^ http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx
- ^ OFCM, 2005: Federal Meteorological Handbook No. 1, Washington, DC., 104 pp.