Compiler bug in Build 1966

To add comments, please log in or register
Doerk Hilger
1082
Doerk Hilger  

The following code compiles in MT5 without errors, in MT4 it reports a "not boolean expression" warning - which is wrong. But when the code is changed in a way that it compiles without errors, only the half of the functions is executed.

//+------------------------------------------------------------------+

//|                                                      BoolBug.mq4 |

//+------------------------------------------------------------------+

#property version   "1.00"

#property strict

//+------------------------------------------------------------------+

//| Demonstration of major bug in MT4 Build 1966                     |

//+------------------------------------------------------------------+

int OnInit()

  {

      main();

      return INIT_FAILED;

  }


bool main()

   {

   //--- This will execute func1() and func2()

   bool result=func1()&func2();

   

   //--- This will compile with a warning message, even if the expression correct

   return func1()&func2();

   

   //-- This would compile instead without a message, but not execute func2()

   return func1()&&func2();

   }


bool func1()

   {

   Print("Function #1");

   return false;

   }

bool func2()

   {

   Print("Function #2");

   return false;

   }


Amir Yacoby
1306
Amir Yacoby  
Maybe I misunderstand your question, but if you mean this part:
   //-- This would compile instead without a message, but not execute func2()
   return func1()&&func2();

then, compilers usually execute only first part of Boolean expression AND if the first part is false, there is no need to execute the 2nd part. 

*-In same manner, they will execute only first part of logical OR if first part is true.


Doerk Hilger
1082
Doerk Hilger  

Isn´t it also a major bug, that func2() is not executed in a boolean operation when the first operand is already false? 

Because:

bool result=func1()&&func2();

as well as 

if (func1() && func2())


would never execute func2(), while

bool result=func1()&func2()

executes both, unless result can also never become true when func1() is already false. 
Doerk Hilger
1082
Doerk Hilger  
Amir Yacoby:
Maybe I misunderstand your question, but if you mean this part:

then, compilers usually execute only first part of Boolean expression AND if the first part is false, there is no need to execute the 2nd part. 

*-In same manner, they will execute only first part of logical OR if first part is true.


I am not sure if this is ISO standard, I know what you mean. But the problem is, that MT4 compiles an actual correct code with hundreds of warning- and also error messages.

Amir Yacoby
1306
Amir Yacoby  
Doerk Hilger:

Isn´t it also a major bug, that f

bool result=func1()&func2()

unc2() is not executed in a boolean operation when the first operand is already false? 

Because:

bool result=func1()&&func2();

as well as 

if (func1() && func2())


would never execute func2(), while

bool result=func1()&func2()

executes both, unless result can also never become true when func1() is already false. 

So, you mean only the 3rd part is a bug, right?

bool result=func1()&func2()

I don't see at as a bug, the operation is different then Boolean AND, even though you assign to a Boolean. It is a matter of further improve the optimization of compiler, but not a bug. 

Amir Yacoby
1306
Amir Yacoby  
Doerk Hilger:

I am not sure if this is ISO standard, I know what you mean. 

Don't know if it's ISO or not, I just know many compilers (most I think) as well as sql compilers work the same way. 

*-It's also advisable to use that to improve performance, taking into account when writing Boolean expressions to put for instance

return VarA && VarB && FuncC() && FuncD() 
Which might execute faster
Doerk Hilger
1082
Doerk Hilger  
Amir Yacoby:
Don't know if it's ISO or not, I just know many compilers (most I think) as well as sql compilers work the same way. 

So, you mean only the 3rd part is a bug, right?

Of course not. Imho only the 3rd part is working correct. Otherwise the code needs to be always like this:

bool result=true;

result&=func1();

result&=func2();

return result;


... uhm no. 4 lines of code instead of 1 ... this can´t be meant for real. 

Amir Yacoby
1306
Amir Yacoby  
I lost you. 
You agree that those two statements would execute only func1() if it's false, and that is correct way to go, right?
bool result=func1()&&func2(); 


if (func1() && func2())
*-In MT5 If func1() is true, it executes them both. Otherwise, only func1.  
Drazen Penic
2788
Drazen Penic  

It's called "Short circuit evaluation", it's common in programming languages, and it's written using double && or ||.
This syntax is used to stop the evaluation when the compiler knows outcome of the boolean expression.

The real question here is why the compiler complains about the single "&". 

That expression should call both functions

Did you check MQL5 documentation on boolean operators if the MQ changed something about "&"? 

Short-circuit evaluation - Wikipedia
Short-circuit evaluation - Wikipedia
  • en.wikipedia.org
Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the function...
Alain Verleyen
37487
Alain Verleyen  

'&' is not a boolean operator it's a bitwise operator.

mql4 and mql5 (still beta) give the exact same warning. This is new warning and it is here to stay (with good reason in my opinion).

Forum on trading, automated trading systems and testing trading strategies

New version of MetaTrader 4 build 1160

Ilyas , 2018.12.20 09:27

For group 2, we will not change behavior.

The type of binary operation is always int for operands of a smaller type.

Use

   //--- This will compile with a warning message, even if the expression correct
   return bool(func1()&func2());
However I am not sure why "bool result=func1()&func2();" doesn't give a warning in the same way, doesn't seem coherent.
Doerk Hilger
1082
Doerk Hilger  
Drazen Penic:

It's called "Short circuit evaluation", it's common in programming languages, and it's written using double && or ||.
This syntax is used to stop the evaluation when the compiler knows outcome of the boolean expression.


Thank you, never heard about it. 

Drazen Penic:

The real question here is why the compiler complains about the single "&". 

That expression should call both functions

Did you check MQL5 documentation on boolean operators if the MQ changed something about "&"? 

It calls both functions, but you´re not allowed to use it in a return command. 

Alain Verleyen:

'&' is not a boolean operator it's a bitwise operator.

mql4 and mql5 (still beta) give the exact same warning. This is new warning and it is here to stay (with good reason in my opinion).

Use

However I am not sure why "bool result=func1()&func2();" doesn't give a warning in the same way, doesn't seem coherent.

Thank you.

12
To add comments, please log in or register