Suppose I have a vector v2 = c(50,30,10,5)
and I would like to calculate the angle between v2
and itself (i.e. the angle should be 0). I use the following codes:
norm_vec = function(x) sqrt(sum(x^2))
Then I call
acos( as.numeric((v2 %*% v2) / (norm_vec(v2) * norm_vec(v2))) )
However, instead of getting 0, I got the following warning message:
Warning message:
In acos(as.numeric((v2 %*% v2)/(norm_vec(v2) * norm_vec(v2)))) :
NaNs produced
I checked the value of as.numeric((v2 %*% v2) / (norm_vec(v2) * norm_vec(v2)))
as it is indeed a numeric of 1. I also checked acos(1)
and the result is 0. What's wrong with my codes? Thanks!