Re: MemberQ

• To: mathgroup at smc.vnet.net
• Subject: [mg68544] Re: MemberQ
• From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
• Date: Wed, 9 Aug 2006 04:19:54 -0400 (EDT)
• Organization: The Open University, Milton Keynes, UK
• References: <eb9pkt\$t4j\$1@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},

The above is only true to one decimal place. I am sure what you have in
mind is 0.7 == 7/10; however this is not the case in computer sciences
because of the way numbers are coded at the hardware level.

0.7 is a machine-precision number so anything, for example, from 0.66 to
0.74 will do it.

> why does MemberQ[Range[0., 1., .1], .7] return False?

0.7 is a machine-precision number so anything, for example, from 0.66 to
0.74 will do it. Now MemberQ looks for strict equality since its a
pattern matching function.

What you must do is either using exact arithmetic

MemberQ[Range[0, 1, 1/10], 7/10]

--> True

or arbitrary precision

MemberQ[Range[0, 1.`20., 0.1`20.], 0.7`20.]

--> True

or machine precision but you must fix the precision anyway. Below, I
typed in MemberQ[Range[0.`2, 1.`2, .1`2], .7`2]: look how the number two
is represented in InputForm.

MemberQ[Range[0, 1.`1.9999999999999998, 0.1`1.9999999999999998],
0.7`1.9999999999999991]

--> True

Section 3.1.4 "Numerical Precision" of the Mathematica Book might be of
interest.
http://documents.wolfram.com/mathematica/book/section-3.1.4

HTH,
Jean-Marc

```

• Prev by Date: Re: Multiplying Elements of arrays: Inner product
• Next by Date: Re: Newbie question about column sums of arrays
• Previous by thread: Re: MemberQ
• Next by thread: RE: Re: MemberQ