Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!news1.google.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post02.iad.highwinds-media.com!newsfe11.iad.POSTED!7564ea0f!not-for-mail
From: Walter Roberson <roberson@hushmail.com>
Organization: Canada Eat The Cookie Foundation
User-Agent: Thunderbird 2.0.0.19 (Windows/20081209)
MIME-Version: 1.0
Newsgroups: comp.soft-sys.matlab
Subject: Re: ismember - getting wrong result
References: <gpb180$5uv$1@fred.mathworks.com> <gpcnal$rr6$1@fred.mathworks.com>
In-Reply-To: <gpcnal$rr6$1@fred.mathworks.com>
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Lines: 22
Message-ID: <K4Jul.132944$2h5.128860@newsfe11.iad>
NNTP-Posting-Host: 24.79.146.116
X-Complaints-To: internet.abuse@sjrb.ca
X-Trace: newsfe11.iad 1237015978 24.79.146.116 (Sat, 14 Mar 2009 07:32:58 UTC)
NNTP-Posting-Date: Sat, 14 Mar 2009 07:32:58 UTC
Date: Sat, 14 Mar 2009 02:33:06 -0500
Xref: news.mathworks.com comp.soft-sys.matlab:524802


Ashwini Deshpande wrote:
 
> I tried another method of rounding off the values for example,
> 
> a=0.1:0.1:1;
> a1 = round(a*10)/10;
> 
> [tf, loc] = ismember((round(0.3*10)/10), a1);
> 
> and it also works well ...


No, that's broken too -- it just hasn't bit you yet.

Unless you are a floating point guru, never compare floating point
numbers for exact equality (which ismember does implicitly): instead,
compare with a tolerance. Even when your code appears to calculate two
floating point number by using the same expression, Matlab is permitted
to rewrite the expressions for that -it- thinks is optimal -- or you
could hit a problem such as "register spill". (If you don't know what
"register spill" is and why it is a problem, you don't know enough
about floating point to know when to break the rules.)