# numerical accuracy & sorting problem

```hi all,

I have encountered a problem that can be illustrated by:

In[129]:=ohla=Sort[raw]
Out[129]=
{{-0.948683, 2., 4}, {0., 3.16228, 1}, {0.316228, 3., 2}, {0.316228, 2.,
3}, {1., 0., 5}, {1., 3.16228, 6}}

at this point, the Sort puts {0.316228, 3., 2} before {0.316228, 2., 3}.

so, it must be that both "0.316228" values are slightly different. but
when I test this, it doesn't turn out so:

In[108]:=ohla[[{3,4}]]
Out[108]={{0.316228, 3., 2}, {0.316228, 2., 3}} In[65]:={a,b}=First/@ %
Out[65]={0.316228, 0.316228}
In[96]:=a===b
Out[96]=True

Further experiments are even more confusing. At full precision,
N[raw,40] shows no differences,
turning the output-cell into an input cell,  copying & pasting as new
input and sorting that makes the problem go away:

In[116]:=
Sort[{{0.3162277660168379, 2., 3}, {-0.9486832980505138, 2., 4}, {1.,
0., 5},
{0., 3.162277660168379, 1},
{0.3162277660168379, 3., 2}, {1., 3.162277660168379, 6}}] Out[116]=
{{-0.948683298050514, 2., 4}, {0., 3.162277660168379, 1},
{0.3162277660168379,
2., 3}, {0.3162277660168379, 3., 2},
{1., 0., 5}, {1., 3.162277660168379, 6}}

At a loss as to how to proceed, I came up with a trick that *seems* to
work for now:
add 10 and subtract 10. from the argument in question : In[132]:=
raw={10+ang[pts[[Take[hull,-2] ]] , pts[[#]] ]-10.,  norm[(pts[[
Last[hull] ]]
-pts[[#]])] ,indices[[#]]  }   &/@idx

Is this guaranteed always to work?
is there a more elegant way?

baffled as always,

wouter.

below this line are sufficient data to reconstruct the whole thing:
-------------------------------------------------------------------
(*** functions used ***)
In[1]:=
myOrdering[z_List]:=(Sort[Transpose[{z,Range[Length[z]]}]]//Transpose)
[[2]] In[2]:=
norm[vec_List]:=Sqrt[vec.vec]
In[3]:=
bary[pts_]:=1/Length[pts] Plus@@pts
In[5]:=
ang[{v1_List,v2_List},v2_|v1_]:=1.
ang[{v1_List,v2_List},v3_List]:=(v2-v1).(v3-v2)/norm[v2-v1]/norm[v3-v2]

(*** start short routine ***)

In[31]:=
pts=N@{{-2, -2, 0}, {-2, -2, 1}, {-2, -1, 1}, {-2, 1, -1}, {-2, 1, 1},
{-2, 2, -2}}
cen=bary[pts]
In[34]:=
dis=norm[#-cen]& /@ pts
idx=myOrdering[ dis]
xfar=Last[idx]
far=pts[[xfar]]

In[38]:=
xsec=Sort[{  ang[{cen,far},pts[[#]] ]  ,   norm[(far-pts[[#]])] ,#
}&/@idx
][[-2,-1]]
Out[50]={6, 5}
hull={xfar,xsec}

raw1={ ang[pts[[Take[hull,-2] ]] , pts[[#]] ] ,  norm[(pts[[ Last[hull]
]]
-pts[[#]])] ,indices[[#]]  }   &/@idx

Sort[raw1]

raw2={10+ang[pts[[Take[hull,-2] ]] , pts[[#]] ]-10.,  norm[(pts[[
Last[hull] ]]
-pts[[#]])] ,indices[[#]]  }   &/@idx

Sort[raw2]

NV Vandemoortele Coordination Center Oils & Fats Applied Research
Prins Albertlaan 79
Postbus 40
B-8870 Izegem (Belgium)
Tel: +/32/51/33 21 11
Fax: +/32/51/33 21 75
vdmcc@vandemoortele.be

```

• Prev by Date: RE: Unexpected behavior with S
• Next by Date: multi-column notebook style
• Prev by thread: RE: Unexpected behavior with S
• Next by thread: multi-column notebook style