The following command returns 1:
ismember(-0.6, -1:.1:.9)
but this next command returns 0:
ismember(-0.1, -1:.1:.9)
even though -0.1 clearly is in -1:.1:.9.
Does anybody know what’s going on?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The problem is that when you start at -1.0 and add 0.1 repeatedly you get a slightly different number than if you specify -0.1 directly. This is because of floating-point error accumulation. Just like 1/3 cannot be represented exactly in decimal (it becomes 0.33333…), many decimal numbers cannot be represented exactly in binary. 0.1 when converted to binary is actually a very close approximation to 0.1. Because there is a slight error when you do arithmetic with this floating point number this small difference accumulates and becomes bigger and bigger.
From http://www.mathworks.com/matlabcentral/newsreader/view_thread/246492: