MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: MemberQ


Bruce Colletti schrieb:
> Re Mathematica 5.2.0.0.
> 
> Since 0.7 is in the set {0.0, 0.1, 0.2,..., 0.9, 1.0}, why does MemberQ[Range[0., 1., .1], .7] return False?
>  
> Thankx.
> 
> Bruce
> 

It returns False, because 0.7 is definitely _not_ a member of
In[1]:= Range[0.,1.,.1]//InputForm

Out[1]//InputForm=
  {0., 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6000000000000001,
  0.7000000000000001, 0.8, 0.9, 1.}

Don't expect exact results, when using inexact numbers.

MemberQ[lst,elm] looks for exactly the same as elm in lst. You can write 
your own MemberQ using == instead of ===, because Equal[] considers 
"very similar" reals to be equal:

In[2]:= sloppyMemberQ = Cases[#1, x_ /; x == #2, 1, 1] =!= {} & ;
In[3]:= sloppyMemberQ[Range[0., 1., 0.1], 0.7]
Out[3]= True

Peter


  • Prev by Date: Re: for higher dimensions?
  • Next by Date: Re: How to get the output of Minimize function in x = a format
  • Previous by thread: Re: MemberQ
  • Next by thread: RE: MemberQ