The cor()
function fails to compute the correlation value if there are extremely big numbers in the vector and returns just zero:
foo <- c(1e154, 1, 0)
bar <- c(0, 1, 2)
cor(foo, bar)
# -0.8660254
foo <- c(1e155, 1, 0)
cor(foo, bar)
# 0
Although 1e155
is very big, it's much smaller than the maximum number R can deal with. It's surprising for me why R returns a wrong value and does not return a more suitable result like NA
or Inf
.
Is there any reason for that? How to be sure we will not face such a situation in our programs?