2
> x <- 1:9
> y <- x + 0.055000
> z <- round(y,2)
> cat(z)
1.05 2.06 3.06 4.05 5.05 6.05 7.05 8.05 9.05

On the numbers 2 and 3, it rounds up to 2.06 and 3.06. The other numbers round down to .05. I was wondering why? If I add 0.0550001 instead of 0.055000, everything rounds to .06.

This happens on both my Mac (Sierra) and PC (Win10/Edu). I run R 3.5.1 on both.

Thanks.

tstorm
  • 21
  • 3
  • 3
    combination of round-to-even and floating point details? `print(((1:9+0.055)-0.055) %% 1,digits=20)` – Ben Bolker Oct 31 '18 at 21:36
  • This is a duplicate post, can't find the link... – zx8754 Oct 31 '18 at 21:39
  • 1
    @zx8754 If Ben is right, it would be a duplicate of [this](https://stackoverflow.com/q/12688717/324364) and [this](https://stackoverflow.com/q/9508518/324364). – joran Oct 31 '18 at 21:40
  • 1
    `options(digits = 22); print(y)` will give a better idea of what the numbers in `y` actually are. – Dason Oct 31 '18 at 21:47

1 Answers1

0

well done spotting this, indeed this is not what I would expect. z <- signif(y,3) solves the problem.

> cat(z)
1.06 2.06 3.06 4.06 5.06 6.06 7.06 8.06 9.06
gaut
  • 5,771
  • 1
  • 14
  • 45
  • ============== Thanks to all for the responses. I see now what's happening to y when I add 0.055000 to x. I'm just not sure why R chooses to add 0.0550000... to numbers 2 and 3, but adds 0.0549999... to the other numbers. It's not discriminating against even or odd. – tstorm Nov 01 '18 at 04:07