PC Review


Reply
Thread Tools Rate Thread

Decimal Comparison

 
 
Typpo
Guest
Posts: n/a
 
      15th Mar 2005
Hi,

I have an ArrayList of decimals, and I want to figure out which two are
the closest (least difference) from one another. I know I can loop
through the decimals, but this is very tedious and some people have told
me there are easier ways.

Here is an example -
ArrayList contains:
1.12
1.15
2.45
2.44

2.44 and 2.45 have the least difference between them. Is there any easy
way to do this?

Thanks.
 
Reply With Quote
 
 
 
 
Bruce Wood
Guest
Posts: n/a
 
      15th Mar 2005
I know of no easier way. You have to have two nested loops that will
compare each decimal with each other one, and keep track of the pair
with the (currently) smallest difference.

 
Reply With Quote
 
Mihai N.
Guest
Posts: n/a
 
      15th Mar 2005
> I know of no easier way. You have to have two nested loops that will
> compare each decimal with each other one, and keep track of the pair
> with the (currently) smallest difference.

If the list is sorted one loop is enough.


--
Mihai Nita [Microsoft MVP, Windows - SDK]
------------------------------------------
Replace _year_ with _ to get the real email
 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      15th Mar 2005
True. You need two nested loops if the list is not guaranteed to be
sorted. If it is guaranteed to be sorted, you need only one pass
through the list. Here is the unsorted
version:

int firstIndex = -1;
int secondIndex = -1;
decimal diff = Decimal.MaxValue;
for (int i = 0; i < list.Count - 1; i++)
{
for (int j = i + 1; j < list.Count; j++)
{
decimal newDiff = Math.Abs(list[i] - list[j]);
if (newDiff < diff)
{
diff = newDiff;
firstIndex = i;
secondIndex = j;
}
}
}
if (firstIndex < 0)
{
... there were 0 or 1 elements in the list ... nothing to compare
}
else
{
... the smallest difference was between values at index firstIndex
and index secondIndex, and the difference was "diff".
}

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparison of text field values wtih Decimal symbol gives error chris Microsoft Access VBA Modules 2 5th Nov 2009 05:46 PM
Text box formatted to General Number with 2 decimal places NOTallowing the decimal place. p-rat Microsoft Access Form Coding 3 14th Jan 2008 05:20 PM
Complex comparison of Columns of Data: Extracting unique records after comparison on 4 levels ap Microsoft Excel Programming 2 23rd Jan 2007 10:12 AM
Make decimal work properly in fixed decimal entry in Excel 2003. =?Utf-8?B?am9obmdpaWk=?= Microsoft Excel Crashes 0 12th Apr 2006 06:05 AM
MD5 generation/comparison and Strings comparison Mike Ji Freeware 0 15th Nov 2004 11:57 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:29 PM.