Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Rounding
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Rounding to the nearest integer=== These six methods are called '''rounding to the nearest integer'''. Rounding a number {{mvar|x}} to the nearest integer requires some tie-breaking rule for those cases when {{mvar|x}} is exactly half-way between two integers β that is, when the fraction part of {{mvar|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 arithmetic|fixed-point]] numbers with [[discrete uniform distribution|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|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 arithmetic|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''' (or '''round half toward positive infinity'''), a tie-breaking rule that is widely used in many disciplines.{{citation needed|reason=This is surprising. A confusion with ''round half away from zero''?|date=June 2017}} That is, half-way values of {{mvar|x}} are always rounded up. If the fractional part of {{mvar|x}} is exactly 0.5, then {{math|1=''y'' = ''x'' + 0.5}} : <math>y = \left\lfloor x + \tfrac12 \right\rfloor = -\left\lceil -x - \tfrac12 \right\rceil = \left\lceil \tfrac12 \lfloor 2x \rfloor \right\rceil</math> For example, 23.5 gets rounded to 24, and β23.5 gets rounded to β23. Some programming languages (such as Java and Python) use "half up" to refer to ''[[#Rounding half away from zero|round half away from zero]]'' rather than ''round half toward positive infinity''.<ref name="java">{{cite web |title=java.math.RoundingMode |url=https://docs.oracle.com/javase/8/docs/api/java/math/RoundingMode.html |publisher=Oracle}}</ref><ref name="python">{{cite web |title=decimal β Decimal fixed point and floating point arithmetic |url=https://docs.python.org/3/library/decimal.html#rounding-modes |publisher=Python Software Foundation}}</ref> 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''' (or '''round half toward negative infinity''') as opposed to the more common ''round half up''. If the fractional part of {{mvar|x}} is exactly 0.5, then {{math|1=''y'' = ''x'' β 0.5}} : <math>y = \left\lceil x - \tfrac12 \right\rceil = -\left\lfloor -x + \tfrac12 \right\rfloor = \left\lfloor \tfrac12 \lceil 2x \rceil \right\rfloor</math> For example, 23.5 gets rounded to 23, and β23.5 gets rounded to β24. Some programming languages (such as Java and Python) use "half down" to refer to ''[[#Rounding half toward zero|round half toward zero]]'' rather than ''round half toward negative infinity''.<ref name="java"/><ref name="python"/> ====Rounding half toward zero==== One may also '''round half toward zero''' (or '''round half away from infinity''') as opposed to the conventional ''round half away from zero''. If the fractional part of {{mvar|x}} is exactly 0.5, then {{math|1=''y'' = ''x'' β 0.5}} if {{mvar|x}} is positive, and {{math|1=''y'' = ''x'' + 0.5}} if {{mvar|x}} is negative. : <math> y = \sgn(x) \left\lceil \left| x \right| - \tfrac12 \right\rceil = -\sgn(x) \left\lfloor -\left| x \right| + \tfrac12 \right\rfloor = \begin{cases} \left\lceil x - \tfrac12 \right\rceil = \left\lfloor \tfrac12 \lceil 2x \rceil \right\rfloor & x \ge 0 \\[5mu] \left\lfloor x + \tfrac12 \right\rfloor = \left\lceil \tfrac12 \lfloor 2x \rfloor \right\rceil & x < 0 \end{cases} </math> 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''' (or '''round half toward infinity'''), a tie-breaking rule that is commonly taught and used, namely: If the fractional part of {{mvar|x}} is exactly 0.5, then {{math|1=''y'' = ''x'' + 0.5}} if {{mvar|x}} is positive, and {{math|1=''y'' = ''x'' β 0.5}} if {{mvar|x}} is negative. : <math> y = \sgn(x) \left\lfloor \left| x \right| + \tfrac12 \right\rfloor = -\sgn(x) \left\lceil -\left| x \right| - \tfrac12 \right\rceil = \begin{cases} \left\lfloor x + \tfrac12 \right\rfloor = \left\lceil \tfrac12 \lfloor 2x \rfloor \right\rceil & x \ge 0 \\[5mu] \left\lceil x - \tfrac12 \right\rceil = \left\lfloor \tfrac12 \lceil 2x \rceil \right\rfloor & x < 0 \end{cases} </math> 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''',{{citation needed|date=November 2020}} 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 (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). ====Rounding half to even==== One may also '''round half to even''', a tie-breaking rule without positive/negative bias ''and'' without bias toward/away from zero. By this convention, if the fractional part of {{mvar|x}} is 0.5, then {{mvar|y}} is the even integer nearest to {{mvar|x}}. Thus, for example, 23.5 becomes 24, as does 24.5; however, β23.5 becomes β24, as does β24.5. This function minimizes the expected error when summing over rounded figures, even when the inputs are mostly positive or mostly negative, provided they are neither mostly even nor mostly odd. This variant of the round-to-nearest method is also called '''convergent rounding''', '''statistician's rounding''', '''Dutch rounding''', '''Gaussian rounding''', '''oddβeven rounding''',<ref>[https://web.archive.org/web/20150603082624/http://mscweb.gsfc.nasa.gov/543web/files/GSFC-X-673-64-1F.pdf Engineering Drafting Standards Manual] (NASA), X-673-64-1F, p90</ref> or '''bankers' rounding'''.<ref>{{cite book |title=Postcards 4 Language Booster: Workbook with Grammar Builder |last1=Abbs |first1=Brian |last2=Barker |first2=Chris |last3=Freebairn |first3=Ingrid |publisher=Pearson Education |year=2003 |isbn=0-13-093904-8 |at=[https://books.google.com/books?id=mpvSnBZTsOgC&dq=%22bankers%20rounding%22&pg=PA85 {{pgs|85}}] |quote=Rounding to the nearest even number is also called 'bankers rounding' because the banks use this technique as well.}} {{pb}} {{cite book |publisher=Microsoft Corporation |title=Microsoft Pascal Compiler for the MS-DOS Operating System User's Guide |year=1985 |at=[https://archive.org/details/microsoft-pascal-3.30-users-guide/page/165/ {{pgs|165}}] |quote=Bankers' rounding is used when truncating real numbers that end with .5; that is, odd numbers are rounded up to an even integer, even numbers are rounded down to an even integer.}}</ref> This is the default rounding mode used in [[IEEE 754]] operations for results in binary floating-point formats. By eliminating bias, repeated addition or subtraction of independent numbers, as in a [[Random walk#One-dimensional random walk|one-dimensional random walk]], will give a rounded result with an error that tends to grow in proportion to the square root of the number of operations rather than linearly. However, this rule distorts the distribution by increasing the probability of evens relative to odds. Typically this is less important{{citation needed|date=December 2021}} than the biases that are eliminated by this method. ====Rounding half to odd==== One may also '''round half to odd''', a similar tie-breaking rule to round half to even. In this approach, if the fractional part of {{mvar|x}} is 0.5, then {{mvar|y}} is the odd integer nearest to {{mvar|x}}. Thus, for example, 23.5 becomes 23, as does 22.5; while β23.5 becomes β23, as does β22.5. This method is also free from positive/negative bias and bias toward/away from zero, provided the numbers to be rounded are neither mostly even nor mostly odd. It also shares the round half to even property of distorting the original distribution, as it increases the probability of odds relative to evens. It was the method used for bank balances in the [[United Kingdom]] when it decimalized its currency<ref>Schedule 1 of the Decimal Currency Act 1969</ref>{{clarify|reason=Needs a complete reference.|date=May 2023}}. This variant is almost never used in computations, except in situations where one wants to avoid increasing the scale of floating-point numbers, which have a limited exponent range. With ''round half to even'', a non-infinite number would round to infinity, and a small {{em|[[denormal]]}} value would round to a normal non-zero value. Effectively, this mode prefers preserving the existing scale of tie numbers, avoiding out-of-range results when possible for numeral systems of even [[radix]] (such as binary and decimal).{{clarify|reason=The ''problem'' would be avoided only for halfway numbers. So, how can this be useful in practice? Any practical example?|post-text=(see [[Talk:Rounding#Round half to odd|talk]])|date=September 2017}}.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Rounding
(section)
Add topic