RE: Re: Re: MemberQ

• To: mathgroup at smc.vnet.net
• Subject: [mg68695] RE: [mg68677] Re: [mg68655] Re: MemberQ
• From: "Erickson Paul-CPTP18" <Paul.Erickson at Motorola.com>
• Date: Thu, 17 Aug 2006 04:17:53 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Do you find that any stranger than the following?

In[7]:=
2 == 2.00000000000000000001
Out[7]=
True

If you don't want Mathematica to make assumptions on your behalf, then
you have to tell it exactly what you do want, as in:

In[9]:=
2 == 2.00000000000000000001``50
Out[9]=
False (* as in the exact number 2 isn't equal to the very accurate
number close to 2 *)
In[12]:=
2. == 2.00000000000000000001``50
Out[12]=
True (* But the real number 2. with limited accuracy is close enough to
the very accurate number close to 2 *)
In[13]:=
2``50 == 2.00000000000000000001``50 (* where two very accurate close
number aren't equal *)
Out[13]=
False
In[10]:=
2 // FullForm
Out[10]//FullForm=
2
In[11]:=
2.00000000000000000001``50// FullForm
Out[11]//FullForm=
2.00000000000000000001`50.30102999566398

So in general, it appears that Mathematica tries to call two numbers
equal if they are close compared the accuracy of the less accurate
number. Some of the postings would indicate that Mathematica doesn't
always succeed in this wrt MemberQ and it would appear that it may be
machine dependant. Still seems best to just avoid the problem or deal
explicitly with it.

As to an earlier question about 2/10 vs 1/5, I think Mathematica is
don't exactly what it says it should in that it simplifies the rational
2/10 to 1/5 as the two rational fraction are equivalent. This is
illustrated by:

In[15]:=
2/10 === 1/5
Out[15]=
True
In[16]:=
2/10 //FullForm
Out[16]//FullForm=
Rational[1,5]

Now it's reasonable that someone may want a set of rationals with a
common denominator (perhaps because it is easier to look at, perhaps
other reasons), but it would seem that this operation is appropriate
most of the time and the burden of extra effort in the other cases is
acceptable.

Paul

-----Original Message-----
From: leigh pascoe [mailto:leigh at cephb.fr]
To: mathgroup at smc.vnet.net
Subject: [mg68695] [mg68677] Re: [mg68655] Re: MemberQ

Peter Pein wrote:
> leigh pascoe schrieb:
> ...
>
behavior?
>> In[15]:=Range[0,1,1/10]
>>              MemberQ[Range[0,1,1/10],2/10]
>>
>> Out[15]=\!\({0, 1\/10, 1\/5, 3\/10, 2\/5, 1\/2, 3\/5, 7\/10, 4\/5,
>> 9\/10, 1}\)
>>
>> Out[16]=True
>>
>> Is the above result correct given that 2/10 doesn't appear in the
>> list produced by the range statement??
>>
>
> Mathematica automagically cancels 2/10.
>
>
>> On the other hand 0.5 can be
>> represented exactly as a binary number, but
>> In[29]:=MemberQ[Range[0,1,1/10],.5]
>>
>> Out[29]=False
>>
>>
>
> 1/2 has head Rational, 0.5 has got head _Real. So they must not be the
same.
>
>
>> LP
>>
>>
>
> I wrote a (very) small package:
>
> http://people.freenet.de/Peter_Berlin/Mathe/NMemberQ/NMemberQ.m
> and
> http://people.freenet.de/Peter_Berlin/Mathe/NMemberQ/NMemberQ.nb
>
> Feel free to add the function NMemberQ to your own collection of
utilities.
>
> Cheers,
>    Peter
>
>
>
>
Dear Peter,

Thanks for your reply. Bruce Colletti was the original poster of this
topic and will probably be interested in your function NMemberQ[]. As
others have pointed out the use of MemberQ to compare fixed precision
(Real) and Rational numbers produces unpredictable (machine and version
dependent) results. My main point was that any such comparison should
produce an error or warning that the comparison is not reliable, rather
than True or False.

It still strikes me as strange that

In[8]:=.7//FullForm
Range[0.,1.,.1][[8]]//FullForm
MemberQ[Range[0.,1.,.1],.7]

Out[8]//FullForm=0.7`

Out[9]//FullForm=0.7000000000000001`

Out[10]=True

returns True for two numbers that Ma tells me are different.

Leigh

```

• Prev by Date: Re: too many special linear matrices
• Next by Date: Memory leak - Table & Export
• Previous by thread: Re: Re: MemberQ
• Next by thread: How to get the output of Minimize function in x = a format