Re: the Union of real numbers
- To: mathgroup at smc.vnet.net
- Subject: [mg7198] Re: [mg7004] the Union of real numbers
- From: "caffa" <caffa at iabg.de>
- Date: Fri, 16 May 1997 02:30:26 -0400 (EDT)
- Organization: iabg.de
- Sender: owner-wri-mathgroup at wolfram.com
Russell Towle wrote: > I am trying to apply the Union function to a list of real numbers, > in which many duplicates occur. However, small differences between > the numbers cause some of the duplicates to be retained instead of > discarded. > > Suppose the list of real numbers is called k, my present work-around > is as follows: > > Union [ Rationalize [ k, 10^-6 ] ]. > > This does discard the duplicates. Is there a better method? Yes. Try: In[1]:= lst = {3.0, 2.001, 2.0, 1.01, 1.02}; In[2]:= eps = 0.02; In[3]:= Union[lst, SameTest->(Abs[#1-#2]<=eps &)] Out[3]= {1.01, 2., 3.} Compare: In[4]:= Union[Rationalize[lst, 0.02]] 51 Out[4]= {1, --, 2, 3} 50 Vittorio G. Caffa Vittorio Caffa IABG, Abt. CC22 Einsteinstr. 20 D-85521 Ottobrunn Tel.: (089) 6088-2054 FAX: (089) 6088-2121 eMail: caffa at iabg.de