Question to the MQL4 masters. Again about Double Compare.

 
Hello!
As you know, not only the correctness of calculations but also the reliability of the written code depends on the programming style and neatness in the code.
We do not write toys, so the reliability of the written program is the very first requirement. Most calculations are carried out in dubles and a correct comparison in the code of
of two real numbers in the program code requires a certain approach and accuracy.
I'm trying to figure out the "right" programming style, hence the question:

For an expression

double a;
double b;

if(a==b) or if(a!=b)
{......} {......}

The developers recommend this
//+------------------------------------------------------------------+
//| Function of comparison of two real numbers. |
//+------------------------------------------------------------------+
bool CompareDouble(double Number1, double Number2)
{
bool Compare = NormalizeDouble(Number1 - Number2, 8) == 0;
return(Compare);
}
//+------------------------------------------------------------------+


Is this code correct?

double a;
double b;

if(a>b) if(a<b)
{......} {......}


Most likely not in the general case. What is the correct way to check it?
In general, what style of working with dubles is more appropriate?
Thank you in advance to everyone who responds.
 

I prefer to use the bigger and smaller digits for comparison whenever possible,

double a;
double b;
 
if(a>b)             if(a<b)
    {......}            {......}

so that I don't have to worry too much about running double to a certain digit

Vol = 156.00000002; 
NormVol = NormalizeDouble(Vol,2); 
156.00

If it's necessary to put the numbers together

if(a==b)    или         if(a!=b)
    {......}                    {......}

then I first convert both numbers to a single digit after the dot

  a = NormalizeDouble(Vol1,2);
  b = NormalizeDouble(Vol2,2);
if(a==b)    или         if(a!=b)
    {......}                    {......}
 
xeon:

I first convert both numbers to the same digit after the dot

  a = NormalizeDouble(Vol1,2);
  b = NormalizeDouble(Vol2,2);
if(a==b)    или         if(a!=b)
    {......}                    {......} 
And when calculating in indicators do you use Normalize and similar preparations?
I am looking through a lot of code, I am learning to program and it is rarely found in the indicator code. That is the question - how to do it properly.
 
VBAG:
xeon:

I first convert both numbers to the same digit after the dot

  a = NormalizeDouble(Vol1,2);
  b = NormalizeDouble(Vol2,2);
if(a==b)    или         if(a!=b)
    {......}                    {......} 
Do you use Normalize and similar preparations in your indicator calculations?
I am looking through a lot of code, I am learning to program and it is rarely found in the indicator code. That is the question - how to do it properly.

I try to avoid it usingNormalizeDouble or "<" and ">" signs, because the indicator, script or Expert Advisor may have discrepancies in double.
 
xeon ,
Thank you for your opinion.
 
Normalise always and there will never be a problem with dubs! ;)
In any comparison, any dabble, and with a deliberately chosen accuracy.

//--- NormalizeDouble
double nd( double value, int precision )
{
    return( NormalizeDouble( value, precision ) );
}
 
//--- MathAbs
double abs( double value )
{
    return( MathAbs( value ) );
}
 
//--- Если value1 равняется value2 до precision знака после запятой, возвращает true, иначе - false.
bool equal( double value1, double value2, int precision = 8 )
{
    return( nd( abs( nd( value1, precision ) - nd( value2, precision ) ), precision ) < nd( MathPow( 0.1, precision ), precision ) );
}

Pay attention to the equal function - if changed slightly:
bool equal( double value1, double value2, int precision = 8 )
{
    return( nd( abs( value1 - value2 ), precision ) < nd( MathPow( 0.1, precision ), precision ) );
}
(remove the normalisation of each number before the comparison), it will give a different result.

For example, equal( 0.123456784, 0.123456776 ) will return true in the first case, but false in the second ;)
 
komposter:
Normalise always and there will never be a problem with dubs! ;)
But there will be performance problems. The problem of comparing numbers to double precision is far-fetched and comes from basic ignorance of the math.
 
Irtron:
The problem of comparing numbers to double precision is far-fetched and comes from basic ignorance of the math.

Let me explain.

Obviously, there is a mix-up of notions. There is a floating-point number type used in intel architecture to represent fixed-precision numbers, which, in particular, include all the values of the trading environment, as the processor has a special thresher for floating-point arithmetic.

In fact, fixed-precision numbers differ very little from integers in terms of machine representation. The presence of a decimal place in this case is conditional. This property is widely used in architectures where floating point arithmetic is not hardware supported, such as ARM, which has spread through use in PDAs and smartphones. Floating arithmetic there has to be emulated with pretty heavy code, which warms up the processor and thus wastes the battery, not to mention the speed of program execution. So whenever it's not possible to do with integers, a fixed format is used. Floating precision is used in the most extreme cases. Or rather, of course, it should be.

Nothing prevents the use of fixed (i.e. integer) arithmetic for market values.

This does not apply to the calculated values of the indicators. A direct comparison is quite sufficient there. Why compare them with any precision?
 
Thanks to komposter and Irtron ! Already decided that nothing more will be written and sat down to invent myself and did not see your posts.
Please see what I wrote with my own cunning hand:
//+------------------------------------------------------------------+
bool EqualDouble(double dN1, double dN2,int Digit)
{
double d1 = NormalizeDouble(dN1,Digit);
double d2 = NormalizeDouble(dN2,Digit);
bool res=false;
res=d1-d2 == 0.0;
return(res);
}
//+------------------------------------------------------------------+
bool LessDouble(double dN1, double dN2,int Digit) // If dN1<dN2
{
double d1 = NormalizeDouble(dN1,Digit);
double d2 = NormalizeDouble(dN2,Digit);
bool res=false;
res=d2-d1 > 0.0;
return(res);
}
//+------------------------------------------------------------------+
bool MoreDouble(double dN1, double dN2,int Digit) // If dN1>dN2
{
double d1 = NormalizeDouble(dN2,Digit);
double d2 = NormalizeDouble(dN2,Digit);
bool res=false;
res=d1-d2 > 0.0;
return(res);
}
//+------------------------------------------------------------------+
Покритикуйте, пожалуйста.
 
VBAG:
Please see what I've scribbled with my sloppy hand:
What values are you trying to compare? Prices, lots or indicator values?
 
Irtron:
VBAG:
Please take a look at what I scribbled with my sloppy hand:
What values are you trying to compare? Prices, lots or indicator values?
I wrote in the beginning: "Trying to get the 'right' programming style. "
In essence, it makes no difference what's in the dubles - only the accuracy of the Digit rounding will vary, depending on the requirements of the variable.
Reason: