I have a really weird problem.
I generated a variable through a calculation. Then I took the number and created a +0.02, -0.02 range (like, Number= 7.74, UpperRange=7.76, LowerRange=7.72).
After that I wrote a for loop, which should check if the Number is between the range. The answer must be minimum 1, because 7,74 is in the range between 7.72-7.76.
But the answer is FALSE.
But if I check if 7.73, or 7.75 is in the range, the answer is TRUE.
It will give me TRUE only with specific numbers? Why?
The code:
number= 7.74
upperRange = number + 0.02
lowerRange = number - 0.02
#
# This does not work!
# The length here is 1, but in real more than 1. For example purposes.
for(i in 1:length(number){
if(number[i] %in% seq(upperRange,lowerRange,0.01){
print("Number is in the range")
}
else{
print("Try again")
}
#
#
# But this works. When I change the number from 7.74 to 7.73, 7.75 or 7.76
number2 = 7.73
for(i in 1:length(number){
if(number2[i] %in% seq(upperRange,lowerRange,0.01){
print("Number2 is in the range")
}
else{
print("Try again")
}
FAQ 7.31
This is a common problem when using floating point numbers.
More data; look at the values --