pharmaverse / blog

Blogging on the latest, greatest and most spectacular stuff happening around the pharmaverse
https://pharmaverse.github.io/blog/
Apache License 2.0
21 stars 9 forks source link

Update rounding blog: add a note for "not to the nearest even" cases (base R round) #88

Closed kaz462 closed 1 year ago

kaz462 commented 1 year ago

round to the nearest even:

> round(1.35, 1)
[1] 1.4
> round(1.45, 1)
[1] 1.4

not to the nearest even:

> round(1.15, 1)
[1] 1.1
> round(1.85, 1)
[1] 1.9

because:

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) is 0 and round(-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 so round(0.15, 1) could be either 0.1 or 0.2).

options(digits = 22)
1.15
[1] 1.149999999999999911182
1.85
[1] 1.850000000000000088818