Why does significant digits function and round function round down in these examples? Result should be 211, but R outputs 210.
signif(x = 210.5, digits = 3)
[1] 210
round(210.5, digits = 0)
[1] 210
Why does significant digits function and round function round down in these examples? Result should be 211, but R outputs 210.
signif(x = 210.5, digits = 3)
[1] 210
round(210.5, digits = 0)
[1] 210
I think this is due to a behavior of R rounding to nearest even.
See ?round
help page
Note that for rounding off a 5, the IEC 60559 standard (see also ‘IEEE 754’) is expected to be used, ‘ go to the even digit ’. Therefore
round(0.5)
is0
andround(-1.5)
is-2
. However, this is dependent on OS services and on representation error (since e.g.0.15
is not represented exactly, the rounding rule applies to the represented number and not to the printed number, and soround(0.15, 1)
could be either0.1
or0.2
).
can you suggest how to solve this issue, so the output rounds to the appropriate number?
ceiling()
should do the job.
ceiling(210.5)
#> [1] 211
Created on 2020-04-22 by the reprex package (v0.3.0)
There is a lot of SO questions on this topic, you could see the inspiration for workaround there:
There is also this blog post and a solution inspired by one on SO
Hope it helps.
I think you'll find the term "appropriate number" is arbitrarily defined. And a very good case can be made that always rounding up in the case of a 0.5 is not the best approach to rounding.
You'll find that all respectable programs use this same round-to-even standard. I would recommend embracing it.
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.