Re: MemberQ

• To: mathgroup at smc.vnet.net
• Subject: [mg68558] Re: [mg68503] MemberQ
• From: Igor Antonio <igora at wolf-ram.com>
• Date: Wed, 9 Aug 2006 04:20:49 -0400 (EDT)
• Organization: Wolfram Research, Inc.
• References: <200608081028.GAA29635@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Bruce Colletti wrote:
> 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

I don't know what function MemberQ uses to identify if something is part
of the list, but it seems to be related to the precision of the 0.7:

In[8]:=
MemberQ[Range[0., 1.0, 0.1], 0.7]

Out[8]=
False

In[9]:=
MemberQ[Range[0., 1.0, 0.1], 0.1]

Out[9]=
True

In[19]:= Range[0.0, 1.0, 0.1] // InputForm

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

Two alternatives:

1) use exact numbers

In[14]:=
MemberQ[Range[0, 1, 1/10], 7/10]

Out[14]=
True

2) Use a condition statement, which allows you to use the == function.
Note that even == will not work in some cases, however.  It will depend
on the precision of the numbers involved.

In[29]:= MemberQ[Range[0., 1.0, 0.1], x_ /; x == 0.7]

Out[29]= True

Igor

--
Igor C. Antonio
Wolfram Research, Inc.
http://www.wolfram.com

To email me personally, remove the dash.

```

• References:
• MemberQ
• From: Bruce Colletti <vze269bv@verizon.net>
• Prev by Date: Re: MemberQ
• Next by Date: Re: Multiplying Elements of arrays: Inner product
• Previous by thread: Re: MemberQ
• Next by thread: Re: MemberQ