问题
Based on a problem I discussed here: https://stackoverflow.com/a/57364028/2725773 I'm wondering what's the tolerance/precision of the which.max function in R.
Specifically, the alternative max.col function has a tolerance of 1e-5, which means that 0.12345 is the same for it as 0.12346.
The help page for max.col suggests an alternative, namely using unname(apply(m, 1, which.max)), which brings me to the questions what's the tolerance of which.max?
回答1:
Fascinating question. I do not know the precise answer. But it's possible to test some very small numbers to see what happens..
# the fourth element is the max
c(1,2,3,4,2) %>% which.max
# [1] 4
vec <- c(1,2,3,4,2)
# how tiny can the numbers become before which.max cannot tell the difference between them?
for(i in 1:30) {
vec <- vec / (10 ^ i)
max_num <- vec %>% which.max
print(vec)
print(max_num)
}
It looks like the smallest these numbers can get to is 1e-300 2e-300 3e-300 4e-300 2e-300 (on the next iteration, which.max cannot tell the difference)
来源:https://stackoverflow.com/questions/57393262/whats-the-tolerance-of-the-which-max-function-in-r