Newbie if statement qestion

 
Hello,

Can someone tell me why the following statement does not work?

if(Bid == SellOpenPrice)


For some reason it returns false most of the time when it is true.

Thank you in advance
 

"Can someone tell me why the following statement does not work?"

Because they are not equal despite your protestations.

Comparing equality of doubles is a known problem because of the way doubles are represented in binary.


When you get stuck, you can change to something like:

if(MathAbs(Bid - SellOpenPrice) < .00001)


Or...

Rosh 2007.05.16 12:13

Use function from Stdlib.mq4

//+------------------------------------------------------------------+
//| right comparison of 2 doubles                                    |
//+------------------------------------------------------------------+
bool CompareDoubles(double number1,double number2)
  {
   if(NormalizeDouble(number1-number2,8)==0) return(true);
   else return(false);
  }
 
phy:

"Can someone tell me why the following statement does not work?"

Because they are not equal despite your protestations.

Comparing equality of doubles is a known problem because of the way doubles are represented in binary.


When you get stuck, you can change to something like:

if(MathAbs(Bid - SellOpenPrice) < .00001)


Or...

Rosh 2007.05.16 12:13

Use function from Stdlib.mq4

//+------------------------------------------------------------------+
//| right comparison of 2 doubles                                    |
//+------------------------------------------------------------------+
bool CompareDoubles(double number1,double number2)
  {
   if(NormalizeDouble(number1-number2,8)==0) return(true);
   else return(false);
  }
Thanks Phy. I will give it try.