0

I'm getting some very bizarre behaviour in R and I cannot figure it out for the life of me. Minimum viable example follows:

x <- c(1, 1, 6, 1)
y <- c(0, 5)
z <- c(4, 0, 0, 2, 0, 5)

x90 <- quantile(x, 0.9)
y90 <- quantile(y, 0.9)
z90 <- quantile(z, 0.9)

c(x90, y90, z90)
round(c(x90, y90, z90), 0)

The output is:

> c(x90, y90, z90)
90% 90% 90% 
4.5 4.5 4.5 
> round(c(x90, y90, z90), 0)
90% 90% 90% 
  5   4   4  

Can someone tell me why 4.5 sometimes rounds to 4, but not always, please?

Some system info:

  • R version: 3.6.2 (2019-12-12)
  • RStudio version: 1.1.456
  • Platform: i386-w64-mingw32/i386 (32-bit)

Thanks in advance,

r2evans
  • 141,215
  • 6
  • 77
  • 149
ramesesjd
  • 181
  • 1
  • 11
  • 4
    Note that `x90==4.5` is FALSE. The problem is using floating point numbers. Computers use binary numbers rather than base 10 decimals and sometimes this means we get different behaviors than we expect. See [this question](https://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal) for a pretty exhaustive answer. – MrFlick Sep 01 '20 at 23:43

1 Answers1

3

By default, R only shows 7 digits of precision; all of x90, y90, and z90 are equal to 4.5 to 7 significant digits, but printing more significant digits shows that x90 is slightly greater than 4.5 (because floating point math is imprecise).

print(c(x90,y90,z90),digits=20)
                  90%                   90%                   90% 
4.5000000000000008882 4.5000000000000000000 4.5000000000000000000 

For deeper understanding you would have to dig into the details of the quantile calculation, but most people (including me) usually leave it at "floating point math is imprecise, live with it".

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453