Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #6731 > unrolled thread
| Started by | DrMajorBob <btreat1@austin.rr.com> |
|---|---|
| First post | 2011-11-25 09:59 +0000 |
| Last post | 2011-11-25 09:59 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.soft-sys.math.mathematica
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Using Equal with Real Numbers DrMajorBob <btreat1@austin.rr.com> - 2011-11-25 09:59 +0000
| From | DrMajorBob <btreat1@austin.rr.com> |
|---|---|
| Date | 2011-11-25 09:59 +0000 |
| Subject | Re: Using Equal with Real Numbers |
| Message-ID | <janotl$b0l$1@smc.vnet.net> |
> (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.
The point is that they do NOT have the same InputForm, as your own outputs
show.
For flexibility, I suggest something like
Clear[memberQ]
memberQ[tol_][list_List, x_] :=
Cases[list, y_ /; Abs[x - y] < tol, {1}, 1] != {}
list1 = Range[0, 1, 0.1];
memberQ[10^-6][list1, #] & /@ {0.6, 0.7}
{True, True}
or
Clear[memberQ]
memberQ[tol_][list_List, x_] := MemberQ[Unitize[list - x, tol], 0]
list1 = Range[0, 1, 0.1];
memberQ[10^-6][list1, #] & /@ {0.6, 0.7}
{True, True}
I think one of those will be pretty fast.
Bobby
On Thu, 24 Nov 2011 05:53:28 -0600, Gabriel Landi <gtlandi@gmail.com>
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}
>
>
>
--
DrMajorBob@yahoo.com
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web