Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

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

Search the Archive

Re: MemberQ

  • To: mathgroup at smc.vnet.net
  • Subject: [mg68530] Re: MemberQ
  • From: "Norbert Marxer" <marxer at mec.li>
  • Date: Wed, 9 Aug 2006 04:18:41 -0400 (EDT)
  • References: <eb9pkt$t4j$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hello

As you can see if you output your list in InputForm

InputForm@Range[0., 1., .1]

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

the specific element does not correspond exactly to 0.7 and therefore
MemberQ returns False. This has to do with the precision of real
numbers.

Nevertheless there are some possibilities to perform your test. Here
are some of them:

Use Integers:
     MemberQ[Range[0, 10, 1]/10, 7/10]

Convert to Integers
     MemberQ[Round /@ (10 Range[0., 1., .1]), 7]

Use Chop, Select and test for the Length:
     Length@Select[Range[0., 1., .1], Chop[# - 0.7] == 0 &] = 0

Use the exact same real number (throughout your calculation):
     r = Range[0., 1., .1];
     MemberQ[r, r[[8]]]

Type in your array by hand (instead of using Range)
     MemberQ[{0.1, 0.2, 0.7}, 0.7]

etc.

They all return True.

Best Regards
Norbert Marxer
www.mec.li

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


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