0

R fails to round the number "126.5". I discovered this by accident.

round(125.5) # = 126, correct
round(126.5) # = 126, wrong
round(127.5) # = 128, correct

I expect that the output of round(126.5) to be 127, but the actual output is 126. R rounds other numbers correctly (see above). Does anybody know what the problem is and how can I fix it?

Gurkenhals
  • 29
  • 7

1 Answers1

2

From documentation ?round -

Note that for rounding off a 5, the IEC 60559 standard 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).

Shree
  • 10,835
  • 1
  • 14
  • 36