Bug in MQL4 double calculation

 
Try this script :

// BUG IN MQL4

double A=1.2751;
double B=1.2735;
int C;

int start()
{
C=(A-B)/0.0016;
if(C != 1)
Comment("BUG IN MQL4 : (1.2751-1.2735)/0.0016 NOT EQUAL TO 1 !!!");
return(0);
}

Hi metaquotes, could you please fix the double calculation bug ?

Thank you
 
I am also having this problem. I did a temporary fix by not using the assignment in the variable declaration, but i think this used to work. Maybe before built 198.
 
what do you mean by "not using the assignement in the variable declaration" ?

The only way I found to avoid this bug was to use : Mathround((A-B)*10000)/16
 
This is likely to be a platform double representation restriction rather than a language bug.
The expression you're trying to evaluate is not necessarily equal to 1 precisely. It's quite possible to be something like 0.9999999999999999.
Although the casting from double to int merely throws away the fraction making the result integer to be 0.

So, firstly, this is not a bug but a known platform restriction.
Secondly, the double to int conversion problem can be easily worked around.

mqlexpert { a t } gmail { d o t } com
 
This is likely to be a platform double representation restriction rather than a language bug.
The expression you're trying to evaluate is not necessarily equal to 1 precisely. It's quite possible to be something like 0.9999999999999999.
Although the casting from double to int merely throws away the fraction making the result integer to be 0.

So, firstly, this is not a bug but a known platform restriction.
Secondly, the double to int conversion problem can be easily worked around.

mqlexpert { a t } gmail { d o t } com






Try to use NormalizeDouble() function.
 
Try to use NormalizeDouble() function.

What for?
To convert 0.9999999999999999 to 0.9999999999999999?
 
Try to use NormalizeDouble() function.

What for?
To convert 0.9999999999999999 to 0.9999999999999999?


Because 1.2751-1.2735=0.0016 and not 0.9999999999999999;
 
Because 1.2751-1.2735=0.0016

Again, not necessarily.
1.2751-1.2735 could be 0.0015999999999998 as well.
Definitely not 0.9999999999999999 though.
 
Because 1.2751-1.2735=0.0016

Again, not necessarily.
1.2751-1.2735 could be 0.0015999999999998 as well.
Definitely not 0.9999999999999999 though.


That is why you should use NormalizeDouble() function.
See https://en.wikipedia.org/wiki/Floating_point
 
Thank you Sub,

In fact NormalizeDouble is much smarter than using Mathround :)
Reason: