Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1997
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1997

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

Search the Archive

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


  • 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.