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:
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".