ArrayBSearch....Baffling?? - page 2

 

angevoyageur - OK, sorry I didn't put the full code in - what you have written is my real code. I attached it wrongly.

Raptor - if I understand your code then it is a work around but you are still only working to a number of digits less than 8. So most brokers now work with 5 digits as standard - if you compare values at a half point either side this is still not as accurate a working with 8 digits which is the max. resolution of MT4.

 

Guys, this is just the beginning - what I want to do is search the array for duplicate values. So if there are 5 array elements equal to 1.61234 then that's what I want to know. So it doesn't fill me confidence when ArrayBSearch can't even find 1 value that I choose accurately!!

This should be a no brainer - how many digits precision do you need for it to get the right number for the search?

 
int start()
  {
//----
   int count=0;
   int array[10] = {0,1,1,6,7,8,7,3,1,9};
   ArraySort(array,WHOLE_ARRAY,0,MODE_ASCEND);
   for(int i=0;i<10;i++)
   {
   int c = ArrayBsearch(array,array[i],WHOLE_ARRAY,0,MODE_ASCEND);
   if(array[i]==7){count++; Print(count,"  ",DoubleToStr(c,0));}
  }
//----
   return(0);
  }
I ran this as a script and it works fine....so Raptor you might be right about double precision!!
 
sd59:

Raptor - if I understand your code then it is a work around but you are still only working to a number of digits less than 8. So most brokers now work with 5 digits as standard - if you compare values at a half point either side this is still not as accurate a working with 8 digits which is the max. resolution of MT4.

Did you try my correct suggestion ? the one with the minus ?

You can get more resolution than 8 digits . . . but it doesn't matter, the fundamental issue is comparing doubles. You need to understand the issue of comparing doubles regardless of this ArrayBsearch() issue. . you should read this thread: Can price != price ? from start to finish, if you don't understand then read it some more and do some research using Google.
 

Raptor, we need to keep this simple. Built in code is meant to help the user - so why, in this situation is so difficult to find a value in an array accurately?

Your code only uses 5/6 digit accuracy.

Also, I thought MT4 precision was limited to 8 digits. Otherwise it just gets ridiculous!

 
sd59: so why, in this situation is so difficult to find a value in an array accurately?
Asked and answered. Read the links: The == operand. - MQL4 forum The problem has nothing to do with mt4. It's the same problem on every computer that uses floating point numbers
 
WHRoeder:
Asked and answered. Read the links: The == operand. - MQL4 forum The problem has nothing to do with mt4. It's the same problem on every computer that uses floating point numbers

Anyway I can't reproduce the problem.

int start()
  {
//----
   int count = 0;
   double ArrayTest[];
   
   for(int i=0;i<=Bars;i++)
   {
      if(count<100)
      {
         ArrayResize(ArrayTest,count+1);
         ArrayTest[count] = NormalizeDouble(iHigh(NULL,PERIOD_H4,i),Digits);
         count++;
      }
      else  
         break;
   }
   ArraySort(ArrayTest);
   for(i=0;i<100;i++)
   {
      Print(i, " ", DoubleToStr(ArrayTest[i],Digits));
   }   
   double value = NormalizeDouble(1.33155,Digits);
   int idx = ArrayBsearch(ArrayTest,value);
   Print("Position of ", DoubleToStr(value,Digits), " is ", idx);
//----
   return(0);
  }
With all tested value I got the good index.
 
sd59:

Raptor, we need to keep this simple. Built in code is meant to help the user - so why, in this situation is so difficult to find a value in an array accurately?

Your code only uses 5/6 digit accuracy.

Also, I thought MT4 precision was limited to 8 digits. Otherwise it just gets ridiculous!

Can you post code, that compiles, and demonstrate your issue ?
 

I think I've sorted it. I don't really know how but it seems to be working.

Thanks for everybody's help.

Reason: