Going crazy with two lines of code

 
Hello:

I would like to ask for some help with a problem I have; I really can't figure out what is wrong with this code. I declare some variables, make a division with them and then, depending on the result, I want to perform some actions.

When I run the code I can see by the Print() statement that the result from the division is 1, but then, the if() statement is not executed. I guess it has something to do with data types, but can't understand what am I missing.


The code (simplified) goes like this:

// ---------------------------- Begin

double A, B;

Print(" (Bid - A) / B = ", (Bid - A) / B ); // And eventually this print the result as 1

if ( (Bid - A) / B == 1.0 ) ... // And this does not get executed

// ---------------------------- End


Initially I made the comparison to 1, instead of 1.0 but I changed it to 1.0 to make sure it was a double value; I have tried also to declare a double variable initialized to 1.0, but hasn't worked either.


Any help will be appreciated

Roberto Valbuena
Spain
 
This is due to precision limitations with float arithmetic... Read this -> https://www.mql5.com/en/articles/1561 for answers.
[Deleted]  

Yep, this situation has driven me nuts before too. My solution now is to always convert my numbers to integers whenever comparitive maths are to be involved. BidInPips = Bid/Point, etc.

int AskInPips=NormalizeDouble(Ask,Digits)/Point;
int BidInPips=NormalizeDouble(Bid,Digits)/Point;
if(AskInPips-BidInPips>3) Print("Spread is now ",AskInPips-BidInPips," pips);


Then revert back to the floating point versions of the variable in question when you actually intend to do something with them like submit a trade order.
 
Thanks a lot, Gordon and Phillip !

I will try to fix right now the precission issue.


Roberto Valbuena

Spain
 

double dPoint = MarketInfo ( Symbol () , MODE_POINT ) ;
double dAsk   = MarketInfo ( Symbol () , MODE_ASK   ) ;
double dBid   = MarketInfo ( Symbol () , MODE_BID   ) ;
int    iAsk   = MathRound  ( dAsk      / dPoint     ) ;
int    iBid   = MathRound  ( dBid      / dPoint     ) ;