ArrayBSearch....Baffling??

 

I have a 101 element array (shown):

  ArrayResize(Array,count);
  ArraySort(Array,WHOLE_ARRAY,0,MODE_ASCEND);
  int c = ArrayBsearch(Array,NormalizeDouble(1.6124,4),WHOLE_ARRAY,0,MODE_ASCEND);
  Print(c):
c = 78???
  ArrayResize(Array,count);
  ArraySort(Array,WHOLE_ARRAY,0,MODE_ASCEND);
  int c = ArrayBsearch(Array,NormalizeDouble(1.5828,4),WHOLE_ARRAY,0,MODE_ASCEND);
  Print(c):
c = 4??
Please can anyone tell me what is happening? 
Shouldn't it search for the value being asked for and return the correct index?
thanks
 
sd59:

I have a 101 element array (shown):

I'm guessing you simply used Print() to show the values in your array ? I'm also guessing you have 5 digits in your data not 4 ? use Print() with DoubleToStr(value, Digits) and show the values in the array again, then read the documentation for ArrayBsearch() and it should make sense . . .
 
sd59:

I have a 101 element array (shown):


her's my code now:

ArrayResize(Array,count);
  ArraySort(Array,WHOLE_ARRAY,0,MODE_ASCEND);
  for(int x=0;x<count;x++)Print(x,"  ",DoubleToStr(BendArray[x],5));
  int c = ArrayBsearch(Array,NormalizeDouble(1.60349,5),WHOLE_ARRAY,0,MODE_ASCEND);
  Print(DoubleToStr(c,0));

I won't list the whole array again just snippets. So using the example above c = 59.

I can go on with other examples but the result is the same. Sometimes (rarely) it will get the right index - it's all very erratic.

 
sd59:


her's my code now:

I won't list the whole array again just snippets. So using the example above c = 59.

I can go on with other examples but the result is the same. Sometimes (rarely) it will get the right index - it's all very erratic.

Maybe it's a double comparison issue, maybe 1.60349 in cell 60 is fractionally greater than 1.60349 so the nearest lower value is found instead, cell 59

You could try this modification . . .

double HalfAPoint = Point / 2.0;    //  <--- added

ArrayResize(Array,count);
  ArraySort(Array,WHOLE_ARRAY,0,MODE_ASCEND);
  for(int x=0;x<count;x++)Print(x,"  ",DoubleToStr(BendArray[x],5));
  int c = ArrayBsearch(Array, 1.60349 + HalfAPoint, WHOLE_ARRAY,0,MODE_ASCEND);  // <--- modified
  Print(DoubleToStr(c,0));
 

To go a step further I changed everything to 8 digit precision.

ArrayResize(Array,count);
  ArraySort(Array,WHOLE_ARRAY,0,MODE_ASCEND);
  for(int x=0;x<count;x++)Print(x,"  ",DoubleToStr(BendArray[x],8));
  int c = ArrayBsearch(Array,NormalizeDouble(1.60156250,8),WHOLE_ARRAY,0,MODE_ASCEND);
  Print(DoubleToStr(c,0));

with example above c = 52!!

I thought it was going to work first of all because the first few tests were correct. Then the above happened...

If this is real it's not good!

 
How is your array "Array" filled with values ?
 
int count = 0;

for(int j=0;j<=Bars;j++)
{
 if(...condition met....)
  {
   Array[count] = iHigh(NULL,Period_H4,j);
   count++;
  }
}
 
RaptorUK:

Maybe it's a double comparison issue, maybe 1.60349 in cell 60 is fractionally greater than 1.60349 so the nearest lower value is found instead, cell 59

You could try this modification . . .

Sorry, my mistake, the modification should of course be . . .

  int c = ArrayBsearch(Array, 1.60349  -  HalfAPoint, WHOLE_ARRAY,0,MODE_ASCEND);  // <--- modified
 
But isn't using 8 digit precision the ultimate test?
 
sd59:
But isn't using 8 digit precision the ultimate test?
Nope, not if it's a double comparison issue . . . what I am suggesting is a work around and if it works it suggests there is a bug with ArrayBsearch() with double array types.
 
sd59:

Try this (not tested) :

int count = 0;

for(int j=0;j<=Bars;j++)
{
 if(...condition met....)
  {
   Array[count] = NormalizeDouble(iHigh(NULL,Period_H4,j),Digits);
   count++;
  }
}

and then

  ArrayResize(Array,count);
  ArraySort(Array,WHOLE_ARRAY,0,MODE_ASCEND);
  for(int x=0;x<count;x++)Print(x,"  ",DoubleToStr(BendArray[x],8));
  int c = ArrayBsearch(Array,NormalizeDouble(1.60156250,Digits),WHOLE_ARRAY,0,MODE_ASCEND);
Reason: