Re: Q: Union and precision.

*To*: mathgroup at smc.vnet.net*Subject*: [mg8544] Re: Q: Union and precision.*From*: bruck at math.usc.edu (Ronald Bruck)*Date*: Sat, 6 Sep 1997 23:16:12 -0400*Organization*: University of Southern California*Sender*: owner-wri-mathgroup at wolfram.com

In article <5uleeq$cp6 at smc.vnet.net>, Gadi Oron <oron at manet.pmmh.espci.fr= > wrote: > Hello again, > > How can I force Union to consider numbers equal in the current > precision as equal? Explanation: > > In:= a=SetPrecision[1.234567,2] > Out:= 1.2 > > In:= b=SetPrecision[1.234561,2] (* Notice difference *) > Out:= 1.2 > > In:= a==b > Out:= True > > In:= Union[{a,b}] > Out:= {1.2,1.2} (* !!! *) > > How does Union evaluates equalities? How can I force it to do it the ri= ght > way?? Hmmm. Interesting. And, perhaps, a little bizarre. When you do FullForm[a] you get 1.234567`2, whereas FullForm[b] returns 1.234561`2. So Mathematica is storing the approximate real with precisio= n information. And asking whether "a==b" is different from asking whet= her "a===b" (since you're asking for equality rather than identity). So apparently Union is using identity to reject duplicates, and you want = it to use simple equality. There is an option to Union, documented in the help browser: Union[a, =8A , SameTest->test] applies test to each pair of elements in the a to determine whether they should be considered the same. So you could make "test" by a check for equality instead of identity: mytest = (#1)==(#2)& Then Union[{a,b},SameTest->mytest] returns {1.2}, as desired. (The actua= l element used seems to be b.) --Ron Bruck