Errors, bugs, questions - page 2208

 

The famous job interview question.

What will be in the i ?

int i = 5;
i = i++ + ++i;
MQL5 outputs 12. MS Visual Studio outputs 13.
 
Aleksey Rodionov:

as it seems to work in C++. That's why I asked, decided to read C++ for example. Same value by one just in difference. It seems to assign 5 to "y" in second case and then add it, while in first example it first adds and then assigns (it took me a long time to figure out how it works =D )

In mql5 they are equal operations, in c++ they are different, below you have just an example of a test on c++ ))

 
. ... Rick D. ... .:

The famous job interview question.

What will be in the i ?

MQL5 gives out 12. MS Visual Studio gives out 13.

Undefined because of the side effect. In this case, a triple effect altogether. Different optimizations may calculate variable values differently. For the sake of optimisation.

 
Konstantin:

In mql5 they are equivalent operations, in c++ they are different, below you were just given an example of a test on c++ ))

Where did you get it from? Did you compare results of operations array[++i]=i and array[i++]=i?

Here's a puzzle for you. What is the difference between postfix operation and prefix operation?

 
. ... Rick D. ... .:

A famous question in a job interview.

A dumb, unnecessary question that only reveals whether the interviewee knows what UB is and has little to do with the topic of discussion.

Aleksey Rodionov:
I'm sitting here thinking, how can the code of prefix form come in handy?

The compiler has enough brains to optimize the postfix form when needed.

 
Slava:

Where did you get the input from? Have you compared results of operations array[++i]=i and array[i++]=i?

Here's a puzzling question. What is the difference between postfix operation and prefix operation?

what does array[++i]=i and array[i++]=i have to do with it, how can I compare what hasn't been in the conversation...

If there are some changes in mql in the framework of the dispute and you know about it, then you as a moderator are obliged to inform the participants of the dialogue, instead of trying to do the syntax knowledge tests, it's definitely not your duty ...

and moreover, show all the instructions in mql help about the difference in postfix and prefix operations, but the tests of previous years show that these operations were equivalent in mql

ps. by the way, i just checked the example from the dialog

int i = 5;
i = i++ + ++i;

build 1816 result == 12

 
TheXpert:

This is a dumb, unnecessary question, which reveals only whether the interviewee knows what UB is and has little to do with the topic of the discussion.

Well, actually the prefix form is faster.

Moreover, I believe that using such constructs in your code is just unacceptable.

First, because of the ambiguity of work in different implementations and, even more, because it's easy to make a mistake in such code and not so easy to make sense of it.

The code should be transparent and understandable at a glance.

 
Georgiy Merts:

Moreover, in my opinion, it is simply unacceptable to use such constructs in code.

Firstly, because of ambiguity of work on different implementations, and even more - because it is easy to make a mistake in such code, and not easy to understand.

The code should be transparent and understandable at a glance.

it's time to change the mql5 help to the form of language conventions and rules

 
Please advise on partial position closing, account type RETAIL_HEDGING. For example, I close half of a position in Expert Advisor and a new order with a smaller lot should open automatically. So, at what point the new order is guaranteed to be in the terminal? Do I correctly understand that it does not have to immediately appear after the PositionClosePartial, and it has to be caught somewhere in OnTrade?
 
Konstantin:

What does array[++i]=i and array[i++]=i have to do with it, how can I compare what didn't happen in the conversation...

If there is a change in mql within the framework of the dispute and you know about it, then as a moderator you are obliged to bring it to the participants of the dialogue, instead of trying to conduct syntax knowledge tests, it's definitely not your duty...

and moreover, show all the instructions in mql help about the difference in postfix and prefix operations, but the tests of previous years show that these operations were equivalent in mql

ps. by the way, i just checked the example from the dialog

build 1816 result == 12

But your examples are purely theoretical. They are intended only for students. No programmer in his/her right mind would release them into production.

Postfix and prefix increment and decrement are actually used first of all in loops. And they are called increments and decrements!

Here are some examples

int i=0;
while(i<ArraySize(array))
   array[i++]=i;

и

int i=0;
while(i<ArraySize(array))
   array[++i]=i;

If you claim that prefix and postfix operations work the same way, then flag in your hands and drum on your neck.

In case of prefix increment, you will get an uninitialized null array element and an array out of range error at the last iteration.


PS. Decided to reread our documentation on this subject in green link https://www.mql5.com/ru/docs/basis/operations/mathoperation

Important note

int i=5;
int k = i++ + ++i;

You may have calculation problems when porting the above expression from one programming environment to another (for example, from Borland C++ to MQL5). In general case, the order of calculations depends on compiler implementation. In practice, there are two ways of implementation of postdecrement (postincrement):

  1. postdecrement (postincrement) is applied to the variable after the whole expression is calculated;
  2. postdecrement (postincrement) is applied to the variable immediately at the place of operation.

In MQL5 we currently implement the first method of calculating the postdecrement (postincrement). But even with this knowledge, it is better not to experiment with using this trick.

Документация по MQL5: Основы языка / Операции и выражения / Арифметические операции
Документация по MQL5: Основы языка / Операции и выражения / Арифметические операции
  • www.mql5.com
Операция инкремента и декремента применяются только к переменным, к константам не применяются. Префиксныe инкремент (++i) и декремент (--k) применяются к переменной непосредственно перед использованием этой переменной в выражении. Могут возникнуть вычислительные проблемы при переносе вышеуказанного выражения из одной среды программирования в...
Reason: