3

I have a huge List, say 10 million items in it, which I'm trying to find a specific value in the list.

Here's my item

struct reference
{
    public double GPST;
    public byte cls;
    public ushort fn;
    public int ret_num;
}

and here's my query code

 List<reference> ref_pts;

 List<reference> result = ref_pts.FindAll(delegate(reference obj) { return obj.GPST == pt.GPST; });

where pt.GPST is the value I want to find in List

Is it just because the size is too big, which Find method not working(always return nothing)?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
高崇軒
  • 117
  • 2
  • 8
  • Yes, It is. I edit the question, thanks for reminding me this. – 高崇軒 Jan 03 '14 at 09:47
  • 3
    No, the reason is not that list is large. The list has no problem handling data of that size. One reason could be the precision problem of doubles that thumbmunkeys suggest, another is that there would be a problem somewhere else in your code so that the list actually doesn't contain the item that you are looking for. – Guffa Jan 03 '14 at 09:48
  • 1
    I tried to find the item by using for loop, and the item can be found this way. I'm so confused. – 高崇軒 Jan 03 '14 at 10:16

1 Answers1

8

You cant compare doubles for equality like this, use an epsilon to compare against:

bool CompareDoubles2 (double A, double B) 
{
   diff = A - B;
   return (diff < EPSILON) && (-diff < EPSILON);
}

EPSILON is a very small number, for details see here and here

Community
  • 1
  • 1
thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110