Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #6669 > unrolled thread
| Started by | Gabriel Landi <gtlandi@gmail.com> |
|---|---|
| First post | 2011-11-24 11:53 +0000 |
| Last post | 2011-11-26 10:10 +0000 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Using Equal with Real Numbers Gabriel Landi <gtlandi@gmail.com> - 2011-11-24 11:53 +0000
Re: Using Equal with Real Numbers A Retey <awnl@gmx-topmail.de> - 2011-11-25 10:09 +0000
Re: Using Equal with Real Numbers Alan <alan.isaac@gmail.com> - 2011-11-25 10:10 +0000
Re: Using Equal with Real Numbers Richard Fateman <fateman@cs.berkeley.edu> - 2011-11-26 10:10 +0000
| From | Gabriel Landi <gtlandi@gmail.com> |
|---|---|
| Date | 2011-11-24 11:53 +0000 |
| Subject | Using Equal with Real Numbers |
| Message-ID | <jalb8i$s6h$1@smc.vnet.net> |
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}
[toc] | [next] | [standalone]
| From | A Retey <awnl@gmx-topmail.de> |
|---|---|
| Date | 2011-11-25 10:09 +0000 |
| Message-ID | <janpgq$b6p$1@smc.vnet.net> |
| In reply to | #6669 |
Hi,
> Using statements like x1=x2, with real numbers is problematic in
> most programming languages.
Actually a single = is Set in Mathematica, what you are talking about is
==, which is Equal or === which is SameQ.
> 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.
>
> 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.
That's not exactly true, since == will recognize the two to be equal:
Range[0, 1, 0.1][[8]] == 0.7
and even SameQ considers Real numbers to be equal if they only differ in
their last binary digit (see the documentation of SameQ):
Range[0, 1, 0.1][[8]] === 0.7
so the problem is not Equal or even SameQ, but the pattern matcher that
MemberQ actually usesimplicitly. The pattern matcher will obviously
consider numbers with different binary representation to be different
and that doesn't even make use of == or ===.
> 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}
I don't think that it is a very useful approach to change the actual
data when all you want is a different comparison operation. What I would
do is something like:
list = Range[0, 1, 0.1];
MemberQ[list, _?(# == 0.7 &)]
or maybe even:
MemberQ[list, _?(Abs[#-0.7]<10^-6 &)]
with a maximal acceptable difference appropriate to the problem at hand.
If you are wondering about the complicated patterns I suggest to learn
about Mathematica's pattern matching. Without a proper understanding of
pattern matching you will find Mathematica to be full of surprises and
by far not as helpful as it could be...
hth,
albert
[toc] | [prev] | [next] | [standalone]
| From | Alan <alan.isaac@gmail.com> |
|---|---|
| Date | 2011-11-25 10:10 +0000 |
| Message-ID | <janpho$b7a$1@smc.vnet.net> |
| In reply to | #6669 |
On Nov 24, 6:53 am, Gabriel Landi <gtla...@gmail.com> wrote:
> (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.
Can you please explain this in more detail? I assume this turn on the
fact that "MemberQ tests for structural matches, not numerical
equality", but how can I see the structural mismatch? Why does it only
apply to 0.7? I.e.,
lst1 = {0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0};
lst2 = Range[0, 1, 0.1];
Map[MemberQ[lst2, #] &, lst1]
=> {True, True, True, True, True, True, True, False, True, True, True}
Thanks,
Alan Isaac
[toc] | [prev] | [next] | [standalone]
| From | Richard Fateman <fateman@cs.berkeley.edu> |
|---|---|
| Date | 2011-11-26 10:10 +0000 |
| Message-ID | <jaqdv7$nh8$1@smc.vnet.net> |
| In reply to | #6669 |
On 11/24/2011 3:53 AM, Gabriel Landi wrote:
> Dear MathGroup members,
...
>
> 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.
This is not really the problem.
One problem is that Mathematica, along with most of the other programs
doing arithmetic with floating point numbers, is that 1/10 is not
exactly representable as a binary float.
The closest double-float is approximately
0.10000000000000000555...
which you can see by SetPrecision[0.1,20]
In fact, the whole range, except for the initial and final values which
are pre-set, are kind of different from what you think they are,
arithmetically speaking. They are, more precisely..
{0, 0.10000000000000000555, 0.20000000000000001110,
0.30000000000000004441, 0.40000000000000002220,
0.50000000000000000000, 0.60000000000000008882,
0.70000000000000006661, 0.80000000000000004441,
0.90000000000000002220, 1.0000000000000000000}
The second problem is that Mathematica's tests for equality (all of
them, I think), are difficult to use since they allow some amount
of tolerance, and cannot be used directly in some situations.
The third problem is that you should not be comparing floats for
equality unless you know what you and your chosen computer system
are doing. See, for example,
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
Yes you can fix some problems by doing exact rational arithmetic.
Given some of the unfortunate choices made in the Mathematica design,
relying on floating-point arithmetic can be dicey.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web