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 the abstract single-argument "round()" function presented and used in the previous sections.

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.
    q = \sgn(y) \left\lceil \left| y \right| \right\rceil = -\sgn(y) \left\lfloor -\left| y \right| \right\rfloor\,
  • round to nearestq is the integer that is closest to y (see below for tie-breaking rules).

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. The following table illustrates these rounding methods:

y round
down
(towards −∞)
round
up
(towards +∞)
round
towards
zero
round
away from
zero
round
to
nearest
+23.67+23+24+23+24+24
+23.50+23+24+23+24+24
+23.35+23+24+23+24+23
+23.00+23+23+23+23+23
000000
−23.00−23−23−23−23−23
−23.35−24−23−23−24−23
−23.50−24−23−23−24−24
−23.67−24−23−23−24−24

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 — whereas stock prices had generally increased in the period. The problem was caused by the index being recalculated thousands of times daily, and always being rounded down to 3 decimal places, in such a way that the rounding errors accumulated. Recalculating with better rounding gave an index value of 1098.892 at the end of the same period.[1]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值