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

 
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 worked around easily.

mqlexpert { a t } gmail { d o t } com
 
Irtron:
This is likely to be a platform double representation restriction rather than a language bug.

Not really.
'int x = double y ?'
 
Zap wrote:
Irtron:
This is likely to be a platform double representation restriction rather than a language bug.

Not really.
'int x = double y ?'
Sometimes called a bug, and more often a bloody nuisance, because there is no way to avoid it.

In a non-engineering platform like MT4, one could consider using a decimal based, fixed-point representation, and that would indeed make the problem easier to explain. All computations would be significantly slower of course, but I suppose that's something one could suffer.

The problem would still remain though, because many of the values we want to deal with cannot be finitely represented in any base.
E.g., 1/3 is a "bad" number: either you never stop adding digits, or you truncate. And once you truncate, you immediately forget that the number came from 1/3, and begin to believe that it's really the value 0.3333 you deal with (or however many digits you prefer to use). So if you then multiply by 3, you get 0.9999.
On the other hand the particular problem that 'flag' presented would not arise, with those numbers and that computation.

It would please me greatly to not having to worry about this particular problem when programming, but whatever level of admiration the MT4 developers are due, to ask them to "correct this bug" is really like asking them to make gold out of clay... or like asking them to make any EA always profitable.

Hmm, why not? Please make it so!
 

Try this out,


double D;


D=(MathRound(((A-B)/0.0016)*10000))/10000;
`
so it looks like there is some residue after all, to whatever it might be.