Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Position and Real Numbers

0 views
Skip to first unread message

Jason Gill

unread,
Jul 22, 1999, 3:00:00 AM7/22/99
to
Folks,
I had a member of my department ask me this question, and I thought
I'd share it with the group.
In short he was using Position to return to the location of a number in
a list, and it wouldn't work in many cases. The example was

In[251]:=
x=Range[-3,2,.05]

Out[251]=
{-3,-2.95,-2.9,-2.85,-2.8,-2.75,-2.7,-2.65,-2.6,-2.55,-2.5,-2.45,-2.4,-2.35,-\

2.3,-2.25,-2.2,-2.15,-2.1,-2.05,-2.,-1.95,-1.9,-1.85,-1.8,-1.75,-1.7,-1.65,-1.\

6,-1.55,-1.5,-1.45,-1.4,-1.35,-1.3,-1.25,-1.2,-1.15,-1.1,-1.05,-1.,-0.95,-0.9,\

-0.85,-0.8,-0.75,-0.7,-0.65,-0.6,-0.55,-0.5,-0.45,-0.4,-0.35,-0.3,-0.25,-0.2,-\

0.15,-0.1,-0.05,0.,0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5,0.55,0.6,0.65,


0.7,0.75,0.8,0.85,0.9,0.95,1.,1.05,1.1,1.15,1.2,1.25,1.3,1.35,1.4,1.45,1.5,

1.55,1.6,1.65,1.7,1.75,1.8,1.85,1.9,1.95,2.}

For a variety or Values, Position returned the null set.

In[286]:=
Position[x,-.2]
Position[x,-1.15]
Position[x,-.15]
Position[x,-.05]
Position[x,.1]

Out[286]=
{}
Out[287]=
{{38}}
Out[288]=
{}
Out[289]=
{}
Out[290]=
{}

The use of InputForm sheds a little light on the situation, but even
that is not consistent, for example the values -1.15 vs. -.15.
I was able to offer a work around that seemed to work, but I am not sure
how reliable it is:
xList=Floor[x*100]/100//N

In anycase, even though I partially understand the behavior, it in
general seems wierd. Is there an easy way to make Position work as
expected for real numbers, or more accurately make Range work as
expected ???

Thanks,
Jason

P.S. Mathematica 3.0 on Win 95.

In[285]:=
x//InputForm
Out[285]//InputForm=
{-3, -2.95, -2.9, -2.85, -2.799999999999999, -2.75, -2.7,
-2.649999999999999, -2.6, -2.549999999999999, -2.5,
-2.45, -2.399999999999999, -2.35, -2.299999999999999,
-2.25, -2.2, -2.149999999999999, -2.1,
-2.049999999999999, -2., -1.95, -1.9, -1.849999999999999,
-1.799999999999999, -1.75, -1.7, -1.649999999999999,
-1.599999999999999, -1.549999999999999, -1.5, -1.45,
-1.399999999999999, -1.349999999999999,
-1.299999999999999, -1.25, -1.199999999999999,
-1.149999999999999, -1.099999999999999,
-1.049999999999999, -1., -0.9499999999999996, -0.9,
-0.85, -0.7999999999999998, -0.75, -0.6999999999999997,
-0.6499999999999999, -0.5999999999999996,
-0.5499999999999998, -0.5, -0.4499999999999997,
-0.3999999999999999, -0.3499999999999996,
-0.2999999999999998, -0.25, -0.1999999999999997,
-0.1499999999999999, -0.09999999999999964,
-0.04999999999999982, 0, 0.05000000000000026, 0.1,
0.1500000000000003, 0.2000000000000001, 0.25,
0.3000000000000002, 0.35, 0.4000000000000003,
0.4500000000000001, 0.5, 0.5500000000000002, 0.6,
0.6500000000000003, 0.7000000000000001, 0.75,
0.8000000000000003, 0.85, 0.9000000000000003,
0.9500000000000001, 1., 1.049999999999999, 1.1, 1.15,
1.2, 1.25, 1.299999999999999, 1.35, 1.4, 1.45, 1.5,
1.549999999999999, 1.6, 1.65, 1.7, 1.75, 1.8, 1.85, 1.9,
1.95, 2.}

Paul Abbott

unread,
Jul 26, 1999, 3:00:00 AM7/26/99
to
Jason Gill wrote:

> Is there an easy way to make Position work as expected for real numbers, or more
> accurately make Range work as
> expected ???

Both work "as expected". With

In[1]:= x=Range[-3,2,.05];

since the elements are distinct, to locate -0.05 you could use a test such as

In[2]:= Position[x,_?(-0.06<#<-0.04&)]

Out[2]= {{60}}

or you could Rationalize the entries

In[3]:= Position[Rationalize[x],Rationalize[-1/20]]

Out[3]= {{60}}

You can make Range give a set of exact values if you give it exact input:

In[4]:= x=Range[-3,2,Rationalize[0.05]];

In[5]:= Position[x,Rationalize[-1/20]]

Out[5]= {{60}}

Moral: If you give Mathematica exact input and it will generally give you exact
ouput. With approximate input you generally get approximate output.

____________________________________________________________________
Paul Abbott Phone: +61-8-9380-2734
Department of Physics Fax: +61-8-9380-1014
The University of Western Australia
Nedlands WA 6907 mailto:pa...@physics.uwa.edu.au
AUSTRALIA http://physics.uwa.edu.au/~paul

God IS a weakly left-handed dice player
____________________________________________________________________


0 new messages