The danger of small numbers

 

Have some code that checks MA crossovers. I discovered that I only got one type (I think it was positive cross overs) but not the other one

Changed the code and it works ok

/*    
    last = ShrtMA[1] - LongMA[1];      // No good.  Yes, "last" is a double
    if(last>0) result = 1;
    if(last<0) result -1;
*/
 
    if(ShrtMA[1] > LongMA[1]) result = 1;     // works
    if(ShrtMA[1] < LongMA[1]) result = -1;    // OK
  
 
ingvar_e:

Have some code that checks MA crossovers. I discovered that I only got one type (I think it was positive cross overs) but not the other one

Changed the code and it works ok

It's not danger of small numbers but danger of double type data.
 
ingvar_e:

Have some code that checks MA crossovers. I discovered that I only got one type (I think it was positive cross overs) but not the other one

Changed the code and it works ok

If you still need to compare the double, read beginning with the words: "If you still need to compare the equality of two real numbers, then you can do this in two different ways. The first...".
Documentation on MQL5: Language Basics / Data Types / Real Types (double, float)
Documentation on MQL5: Language Basics / Data Types / Real Types (double, float)
  • www.mql5.com
Language Basics / Data Types / Real Types (double, float) - Reference on algorithmic/automated trading language for MetaTrader 5
 

The code I have given above as "working" is not good.

I still found some inconcistencies in behavior so I made the following changes to the code.

Result:

Consistent behavior, a lot better results

   
double mult = 10000;

    w1 = ShrtMA[1] * mult;
    w2 = LongMA[1] * mult;
    if(w1 > w2) result = 1;
    if(w1 < w2) result = -1;

 

I had similar problems in C++. In some cases comparing double value with 0 gave incorrect result. However, comparing with 0.0 was working fine and as expected.

I have no idea how mql5 compiler interprets code, but try this: 

double last = ShrtMA[1] - LongMA[1];
if(last>0.0) result = 1;
if(last<0.0) result -1;
Share results please, I am really interested about result of this in mql5.
 
Better to understand why, than try everything and anything.
 
angevoyageur:
Better to understand why, than try everything and anything.
  Out of curiosity, how would you code it??
 
Dr.Trader:

I had similar problems in C++. In some cases comparing double value with 0 gave incorrect result. However, comparing with 0.0 was working fine and as expected.

I have no idea how mql5 compiler interprets code, but try this: 

Share results please, I am really interested about result of this in mql5.
I can only say that as far as I can judge this code works ok. I have other areas to concentrate on, so I will leave this as is unless someone has some objections as to the validity of the code
 
ingvar_e:
  Out of curiosity, how would you code it??
    double epsilon=_Point/2.;
    if(ShrtMA[1]-LongMA[1] > epsilon) result = 1;
    if(LongMA[1]-ShrtMA[1] > epsilon) result = -1;
 
angevoyageur:
Thanks.  Interesting. One thing.   You divide by  2.    Is that a typo or or does the decimal point signify something?
 
ingvar_e:
Thanks.  Interesting. One thing.   You divide by  2.    Is that a typo or or does the decimal point signify something?
It means 2 is a double, but it's not mandatory in this case, just an habit.
Reason: