Re: Re: Q: Union and precision.

*To*: mathgroup at smc.vnet.net*Subject*: [mg8594] Re: [mg8544] Re: Q: Union and precision.*From*: David Withoff <withoff>*Date*: Sun, 7 Sep 1997 22:13:13 -0400*Sender*: owner-wri-mathgroup at wolfram.com

> > 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 right > > 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 precision > information. And asking whether "a==b" is different from asking whether > "a===b" (since you're asking for equality rather than identity). You should be able to get what you want using the SameTest option (I think that this has already been suggested.) The default value of the SameTest option in Union is equivalent to the test used in OrderedQ or in Order, and makes distinctions between any expressions that are not completely identical. This test includes consideration of the digits that are exposed by FullForm. Such strict comparison is needed because, for logical consistency, the comparison test must be transitive. In many cases you can use SameTest->Equal or SameTest->SameQ if you only need to look for numerically significant differences, but those comparisons are not transitive. The Union function, like OrderedQ and Order, makes distinctions that are beyond the resolution of SameQ or Equal. Dave Withoff Wolfram Research