# Double trouble Bug?

114484

There is strange and serious problem with double arithmetics.
In following example, if x=0.0002 then x=y is true.
However, if x is calculated x=1.2012 - 1.201 = 0.0002 then result is false.

double y=0.0002;
double x;

x=0.0002;
x=1.2012-1.201;

Print("x1=",x);
if(x == y)
Print("x2=",x);
else
Print("x3=",x);

printout if x=0.0002
2005.11.01 19:51:50 merlin4xz6 EURUSD,M1: x2=0.0002
2005.11.01 19:51:50 merlin4xz6 EURUSD,M1: x1=0.0002

printout if x=1.2012 - 1.201
2005.11.01 19:51:50 merlin4xz6 EURUSD,M1: x3=0.0002
2005.11.01 19:51:50 merlin4xz6 EURUSD,M1: x1=0.0002

The subtraction shows:
Print(DoubleToStr(1.2012-1.201,8));
2005.11.01 20:54:54 merlin4xz6 EURUSD,M1: 0.00020000

I have 184 Buid 24 Oct. 2005

The problem shows even when y=0.2 and x=1.2 - 1;
114484

In the interim you could use the following:

// set up factor to convert to Pip value
double iPipsFactor=MathPow(10, Digits);

double y=2; // and testing on chart EURUSD
double x;

double Price1=1.2012, Price2=1.201;

Price1*=iPipsFactor;
Price2*=iPipsFactor;
x=Price1-Price2;
Print("x1=",x);
if(x == y) Print("x2=",x);
else Print("x3=",x);
114484

In the interim you could use the following:

// set up factor to convert to Pip value
double iPipsFactor=MathPow(10, Digits);

double y=2; // and testing on chart EURUSD
double x;

double Price1=1.2012, Price2=1.201;

Price1*=iPipsFactor;
Price2*=iPipsFactor;
x=Price1-Price2;
Print("x1=",x);
if(x == y) Print("x2=",x);
else Print("x3=",x);

Unfortunately I am using math for other than pips calculation.
So, a simple expression like this should work!

if(1.2-1.0 == 0.2)
Print("true");
else
Print("false");

I have reinstal Build 184 to latest 28 Oct. 2005
114484

before compare use NormalizeDouble function. You cannot direct compare 2 doubles - there is known problem in the programming
114484

before compare use NormalizeDouble function. You cannot direct compare 2 doubles - there is known problem in the programming

Thanks Slawa,
I guess I am spoiled from using PERL as programming language for too long.

However, I would [not] expect to round defined variables 1.2 or 1.0 because there is logically nothing to be rounded after the math operation is performed . I would expect to round only if a result may be something like 1.23333333333333.

I would recommend that since MQL supports only 8 decimal places that there would be building in Normalization to 8 decimal places and that defined double variables would be taken as they are written, especially if MQL already knows how to round, lets say, 1.2000 to 1.2. It is clear that MQL knows exact amount of the decimal places and therefore, NormalizeDouble() function is redundant.

In addition, to be consistent -- if I perform DoubleToStr(1.2012-1.201, 8) I would expect that a result would be [not ] normalized and would show exact result as used for evaluation if(1.2012-1.201 == 0.0002). Obviously MQL is automatically normalizing in some expressions and not in others which makes this problem appear as the BUG.

Just suggestion since MQL is your proprietary language.
114484

The double trouble precision bug could be in the external library that the MT4 program uses?
114484

The double trouble precision bug could be in the external library that the MT4 program uses?

of course. there is common problem
114484

The double trouble precision bug could be in the external library that the MT4 program uses?

of course. there is common problem

Can it be fixed Slawa?
Until I have learn about this problem it was gravely influencing development of my indicator scheme which is based on 1 pip detection.

I am sure that others have similar problems without even knowing it.
As I said above, the most frustrating is that if you suspect that a double should be rounded and you test print it as 8 decimal point string and there are all "0" then you have no place to go.
114484

it cannot be fixed automatically. use NormalizeDouble function
114484

Hello all,

I see that two months after the first post on this topic this bug is still present and that's a shame. In my case I just want to compare to doubles if one is greater than the other the result is randomly, for instance 1.2121 sometimes can be > than 1.2121 others don't you never know. I already used normalizedouble on both variables but the result is the same. For me this bug affects me a lot in various key points of my EA's. Is it possible to expect a final fix for this? Since it's a well known bug it should be fixed.

Thank you.
Cleon
114484

once more. THERE IS NOT OUR BUG. there is known intel co-processor calculation problem. and there is well known workaround - use normalization. yourself. we cannot normalize your numbers silently.