MathGroup Archive 2011

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

Search the Archive

Re: Using Equal with Real Numbers

  • To: mathgroup at smc.vnet.net
  • Subject: [mg123166] Re: Using Equal with Real Numbers
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Fri, 25 Nov 2011 04:55:14 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201111241153.GAA28857@smc.vnet.net> <6A41692C-6AC4-4F55-9A6A-E292D36265DA@mimuw.edu.pl> <E6C5E416-42E7-4C8D-BB5C-F8F7EFD9CA24@gmail.com>

This is really the same problem. You don't give the definition of f, but 
suppose we use your original example and define:

Clear[f]

f[0.7] = 1; f[x_] := 0

Then

f /@ Range[0, 1, 0.1]

{0,0,0,0,0,0,0,0,0,0,0}

This is because this sort of definition is pattern based as in your 
original example. So to make it work you need to do something like:

Clear[f]

f[x_] /; x == 0.7 = 1; f[x_] := 0

f /@ Range[0, 1, 0.1]

{0,0,0,0,0,0,0,1,0,0,0}

as expected.

Andrzej Kozlowski


On 24 Nov 2011, at 17:09, Gabriel Landi wrote:

> Dear Andrzej,
>
> Indeed, your solution solves the problem of MemberQ.
> However, there are other situations where I have encountered similar 
problems. Here is an example:
>
> list=Range[0,1,0.1];
>
> Do[ f[x] = x^2, {x,list}]
>
> Now, even though f[0.6]=0.36, f[0.7] is undefined, for the very same 
reason.
>
> Best regards,
>
> Gabriel Landi
>
> On Nov 24, 2011, at 10:42 AM, Andrzej Kozlowski wrote:
>
>> MemberQ does not test for mathematical equality but only for matching 
(as pattern). It's easy to write a function that will test whether 
something is Equal (in Mathematica's sense, of course)   to an element 
of a list e.g.:
>>
>> memberQ[l_List, a_] := Or @@ Thread[l == a]
>>
>> Now, repeating your steps:
>>
>> list1 = Range[0, 1, 0.1];
>>
>> {memberQ[list1, 0.6], memberQ[list1, 0.7]}
>>
>> {True,True}
>>
>> Andrzej Kozlowski
>>
>>
>>
>> On 24 Nov 2011, at 12:53, Gabriel Landi wrote:
>>
>>> Dear MathGroup members,
>>>
>>> Using statements like x1=x2, with real numbers is problematic in

>>> most programming languages.
>>> Below I briefly discuss an example with Mathematica and then show 
the
>>> rather truculent solution that I've come up with.
>>> I would love to hear your comments on this and perhaps other (likely

>>> better) solutions.
>>>
>>> Best Regards,
>>>
>>> Gabriel Landi
>>>
>>>
>>> 
--------------------------------------------------------------------------

>>> 
--------------------------------------------------------------------------

>>> 
--------------------------------------------------------------------------

>>> ---
>>>
>>> Consider:
>>>
>>> In[187]:= list1 = Range[0, 1, 0.1]
>>> Out[187]= {0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.}
>>>
>>> Using InputForm we see that:
>>>
>>> In[188]:= list1 // InputForm
>>>
>>> Out[188]//InputForm={0., 0.1, 0.2, 0.30000000000000004, 0.4, 0.5,
>>> 0.6000000000000001, 0.7000000000000001,
>>> 0.8, 0.9, 1.}
>>>
>>> That is, 0.3, 0.6 and 0.7 have some round-off error.
>>>
>>> Now:
>>>
>>> In[200]:= {MemberQ[list1, 0.6], MemberQ[list1, 0.7]}
>>> Out[200]= {True, False}
>>>
>>> (This actually depends on the OS and perhaps other things). The point is
>>> that he recognizes 0.6 as a member of list1 but not 0.7, even though
>>> both have the same InputForms.
>>> This issue, as you may imagine, prohibits one from using functions that
>>> implicitly make use of =, when dealing with real numbers.
>>>
>>> Here is my solution:
>>>
>>> range[xi_, xf_, df_] := N@Rationalize@Range[xi, xf, df]
>>>
>>> That is, I redefine the range function. It first rationalizes the
>>> entries and then transform them into numeric quantities. Not only is
>>> this crude, but is likely quite slow for long lists. Notwithstanding, it
>>> does solve the problem in the previous example:
>>>
>>> In[190]:= list2 = range[0, 1, 0.1]
>>> Out[190]= {0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.}
>>>
>>> In[191]:= list2 // InputForm
>>> Out[191]//InputForm= {0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9,
>>> 1.}
>>>
>>> In[201]:= {MemberQ[list2, 0.6], MemberQ[list2, 0.7]}
>>> Out[201]= {True, True}
>>>
>>>
>>>
>>
>




  • Prev by Date: Re: Aligning 2 Sets of Axes at {0,0}; Rotated & Standard Position
  • Next by Date: Re: What is the point of having Initializations in DynamicModule and Manipulate?
  • Previous by thread: Re: Using Equal with Real Numbers
  • Next by thread: Re: Using Equal with Real Numbers