MathGroup Archive 1997

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Q: Union and precision.

  • To: mathgroup at
  • Subject: [mg8544] Re: Q: Union and precision.
  • From: bruck at (Ronald Bruck)
  • Date: Sat, 6 Sep 1997 23:16:12 -0400
  • Organization: University of Southern California
  • Sender: owner-wri-mathgroup at

In article <5uleeq$cp6 at>, Gadi Oron <oron at

> 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=
> 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=
information.  And asking whether "a==b" is different from asking whet=
"a===b" (since you're asking for equality rather than identity).

So apparently Union is using identity to reject duplicates, and you want =
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=
element used seems to be b.)

--Ron Bruck

  • Prev by Date: File under <Alt>F: "doesn't exist", but <Ctrl>O: no problem !
  • Next by Date: Re: Q??: Solve::"eqf": "False is not a well-formed equation
  • Previous by thread: Re: Q: Union and precision.
  • Next by thread: Re: Re: Q: Union and precision.