If it's for comparison then you could easily create your own function:
bool checkDoubles(double a, double b, string check)
{
if(check==">"){
if (a - b > Point / 2)return(1);elsereturn (0);
}elseif(check=="<"){
if (b - a > Point / 2)return(1);elsereturn (0);
}elseif(check==">="){
if (a - b > -Point)return(1);elsereturn (0);
}if(check=="<="){
if (b - a > -Point)return(1);elsereturn (0);
}elseif(check=="!="){
if (MathAbs(a - b) > Point / 2)return(1);elsereturn (0);
}else {
Print("Sorry you've entered a wrong check value");
}
return (0);
}
The use of "Point" or "Point/2.0" is not a very good difference value, IMO. The roundoff error introduced by NormalizeDouble (which I got burned by today), is certainly much smaller than 8 digits, more likely 15 digits.
Given the previous tips, and made some changes and put together the following routine which seems to work fine (even using the "diff" to 15 decimal places), although it hasn't been rigorously tested yet:
//+------------------------------------------------------------------+bool AvsB(double A, string checkStr, double B)
{
//checkStr = StringTrimLeft(StringTrimRight(checkStr));
double diff = 0.000000000000001; // 15 decimal places//double diff = 0.000000005;//double diff = 0.00000001;if (checkStr == ">" ){if (A - B > diff)return(true);elsereturn(false);}
elseif(checkStr == "<" ){if (B - A > diff)return(true);elsereturn(false);}
elseif(checkStr == ">="){if (A - B > -diff)return(true);elsereturn(false);}
elseif(checkStr == "<="){if (B - A > -diff)return(true);elsereturn(false);}
elseif(checkStr == "!="){if (MathAbs(A - B) > diff)return(true);elsereturn(false);}
elseif(checkStr == "=" || checkStr == "=="){if (MathAbs(A - B) < diff)return(true);elsereturn(false);}
else {Print("Sorry, bad usage: AvsB(A, checkStr, B). Wrong checkStr value: ",checkStr);}
return(false);
} // end of AvsB//+------------------------------------------------------------------+
Here is a check of the obvious:
if (1.34929 == NormalizeDouble(1.34929 , 5)) Alert("MT4 Pass");
elseAlert("MT4 FAIL. ROUNDOFF BUG"); // Yes, this is what MT4 does, a fail.if (AvsB(1.34929 ,"==", NormalizeDouble(1.34929 , 5))) Alert("AvsB Pass"); // It does pass using the AvsB routine!elseAlert("AvsB FAIL. ROUNDOFF BUG");
Here's yet another possible routine that can compare, but can also internally normalize either A and/or B, and will also relax the comparison difference (of A-B or B-A) to a larger number based on "digits". I doubt this routine is necessary compared to the simple "AvsB" above, but it's offered for your use as desired:
//+------------------------------------------------------------------+bool AvsB_nA_nB_digits(double A, string checkStr, double B, bool normalizeA, bool normalizeB, int digits)
{
//checkStr = StringTrimLeft(StringTrimRight(checkStr));
if (normalizeA) A = NormalizeDouble(A,MathMin(8,digits));
if (normalizeB) B = NormalizeDouble(B,MathMin(8,digits));
double diff;
switch(digits)
{
case0 : diff = 0.5; break; // Or 1.0 ??case1 : diff = 0.1; break;
case2 : diff = 0.01; break;
case3 : diff = 0.001; break;
case4 : diff = 0.0001; break;
case5 : diff = 0.00001; break;
case6 : diff = 0.000001; break;
case7 : diff = 0.0000001; break;
case8 : diff = 0.00000001; break;
case9 : diff = 0.000000001; break;
case10 : diff = 0.0000000001; break;
case11 : diff = 0.00000000001; break;
case12 : diff = 0.000000000001; break;
case13 : diff = 0.0000000000001; break;
case14 : diff = 0.00000000000001; break;
default : diff = 0.000000000000001; break; // 15 decimal places max (I think)
}
if (checkStr == ">" ){if (A - B > diff)return(true);elsereturn(false);}
elseif(checkStr == "<" ){if (B - A > diff)return(true);elsereturn(false);}
elseif(checkStr == ">="){if (A - B > -diff)return(true);elsereturn(false);}
elseif(checkStr == "<="){if (B - A > -diff)return(true);elsereturn(false);}
elseif(checkStr == "!="){if (MathAbs(A - B) > diff)return(true);elsereturn(false);}
elseif(checkStr == "=" || checkStr == "=="){if (MathAbs(A - B) < diff)return(true);elsereturn(false);}
else {Print("Sorry, bad usage: AvsB(A, checkStr, B). Wrong checkStr value: ",checkStr);}
return(false);
} // end of AvsB_nA_nB_digits//+------------------------------------------------------------------+
The use of "Point" or "Point/2.0" is not a very good difference value, IMO. The roundoff error introduced by NormalizeDouble (which I got burned by today), is certainly much smaller than 8 digits, more likely 15 digits.
You want the biggest value that can not be considered roundoff error or equivalently, the smallest value that can not be considered a price change. Since prices can only change by a multiple of point, point/2 is just that.
The double value from the broker could be anywhere from 1.234575000000000000 through 1.23458499999999999 and still be considered the same 1.23458 price.
If you had used this, you wouldn't have had the problem:
*Shakes his tie into position with two fingers* Only use normalize double in calculations involving a double value not just everywhere there is a double.
tonny: *Shakes his tie into position with two fingers* Only use normalize double in calculations involving a double value not just everywhere there is a double.
Or just don't use NormalizeDouble() at all, most of the time when it is used there is absolutely no need to use it . . . error 4107 can be addressed by using NormalizeDouble(), but there are other ways. Any price that comes from Predefined Variables or timeseries functions will never need to be Normalized, neither will the result of an integer multiplied by Point.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
If it's for comparison then you could easily create your own function:
It's just a thought.
If it's for comparison then you could easily create your own function:
It's just a thought.
It's a good thought, thank you for it :-)
The use of "Point" or "Point/2.0" is not a very good difference value, IMO. The roundoff error introduced by NormalizeDouble (which I got burned by today), is certainly much smaller than 8 digits, more likely 15 digits.
Given the previous tips, and made some changes and put together the following routine which seems to work fine (even using the "diff" to 15 decimal places), although it hasn't been rigorously tested yet:
Here is a check of the obvious:
Here's yet another possible routine that can compare, but can also internally normalize either A and/or B, and will also relax the comparison difference (of A-B or B-A) to a larger number based on "digits". I doubt this routine is necessary compared to the simple "AvsB" above, but it's offered for your use as desired:
The use of "Point" or "Point/2.0" is not a very good difference value, IMO. The roundoff error introduced by NormalizeDouble (which I got burned by today), is certainly much smaller than 8 digits, more likely 15 digits.
You want the biggest value that can not be considered roundoff error or equivalently, the smallest value that can not be considered a price change. Since prices can only change by a multiple of point, point/2 is just that.
The double value from the broker could be anywhere from 1.234575000000000000 through 1.23458499999999999 and still be considered the same 1.23458 price.
If you had used this, you wouldn't have had the problem:
if (a > b)if (a >= b)if (a != b)should we avoid using normalisedouble ??
or may be.. i have a thought that we can use MathRound function
ex . double x= ( MathRound ( 1.37883 * 100000)) / 100000 ;
so we can make function
*Shakes his tie into position with two fingers* Only use normalize double in calculations involving a double value not just everywhere there is a double.
should we avoid using normalisedouble ??
or may be.. i have a thought that we can use MathRound function
ex . double x= ( MathRound ( 1.37883 * 100000)) / 100000 ;
You still end up with a double and still the possibility of price != price
I arrived at this solution which turns doubles into ints for the purpose of comparing doubles . . .
so that . . .
will never be true.