Arithmetic/Rounding

From Wikibooks, open books for an open world
Jump to: navigation, search

Rounding[edit]

To round a number, you need to look at the digits right of the decimal. If the first digit right of the decimal is equal or higher than 5 in rank (see Spelling out numbers), we add 1 to the integer part of the number, lopping off digits right of the decimal; this is known as rounding up. If the first digit right of the decimal place is less than 5 in rank, we simply rewrite the number again excluding all the digits right of the decimal place; this is known as rounding down. Note that after the number is rounded up or down all of the digits right of the decimal are discarded and only the digits left of the decimal are written.

Rounding to integer[edit]

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

  • round down (or take the floor, or round towards minus infinity): q is the largest integer that does not exceed y.
    q = \mathrm{floor}(y) = \left\lfloor y \right\rfloor = -\left\lceil -y \right\rceil\,
  • round up (or take the ceiling, or round towards plus infinity): q is the smallest integer that is not less than y.
    q = \mathrm{ceil}(y) = \left\lceil y \right\rceil = -\left\lfloor -y \right\rfloor\,.* round towards zero (or truncate, or round away from infinity): q is the integer part of y, without its fraction digits.
    q = \mathrm{truncate}(y) = \sgn(y) \left\lfloor \left| y \right| \right\rfloor = -\sgn(y) \left\lceil -\left| y \right| \right\rceil\,
  • round away from zero (or round towards infinity): if y is an integer, q is y; else q is the integer that is closest to 0 and is such that y is between 0 and q. The signum function is used to determine the sign
    q = \sgn(y) \left\lceil \left| y \right| \right\rceil = -\sgn(y) \left\lfloor -\left| y \right| \right\rfloor\,
  • round to nearest: q is the integer that is closest to y.

The first 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 from the same limiting value (0, +∞, or −∞).

If y is positive, round-down is the same as round-towards-zero, and round-up is the same as round-away-from-zero. If y is negative, round-down is the same as round-away-from-zero, and round-up is the same as round-towards-zero. In any case, if y is integer, q is just y. Here the table with the rounding methods:

y round
down
(towards −∞)
round
up
(towards +∞)
round
towards
zero
round
away from
zero
round
to
nearest
+13.67 +13 +14 +13 +14 +14
+13.50 +13 +14 +13 +14 +14
+13.35 +13 +14 +13 +14 +13
+13.00 +13 +13 +13 +13 +13
0 0 0 0 0 0
−13.00 −13 −13 −13 −13 −13
−13.35 −14 −13 −13 −14 −13
−13.50 −14 −13 −13 −14 −14
−13.67 −14 −13 −13 −14 −14

The choice of rounding method can have a very significant effect on the result.