Features of the mql4 language, subtleties and techniques - page 30

 
Nauris Zukas:

Hello!
What is the correct way to use "double" in the "for" loop? There seems to be no prohibition on using "double" in the help.

Anyway, my conclusion is that "double" is not recommended in the"for" loop because of lack of normalization of ExpressionZ after each iteration. This can lead to at least 2 problems:
1) Wrong number of iterations;
2) Wrong results if you use ExpressionZ to calculate in a statement.

Документация по MQL5: Основы языка / Операторы / Оператор цикла for
Документация по MQL5: Основы языка / Операторы / Оператор цикла for
  • www.mql5.com
. Все повторяется, пока выражение2 не станет ложным. Если оно ложно, цикл заканчивается и управление передается следующему оператору. ВыражениеЗ вычисляется после каждой итерации. могут отсутствовать, однако разделяющие их точки с запятыми (;) опускать нельзя. Если опущено...
 
Nauris Zukas:

Thank you, I understand why this happens, that's why I tried to normalise. It didn't work for me. Alexey Viktorov option didn't fix it either, values remain non-normalized. So the question remains, can "double" be used in a loop and always get the correct number of iterations?

Multiply by 10 to the right degree until you get integers. At first I struggled too, now I even convert price to integers and work with integers or rounding to integers. I have fewer errors, better readability.

In general, as one programmer said after receiving summaries from the Exchange, I do not understand why numbers of a certain digit capacity are represented by real numbers, rather than integers. This is not logically correct. ))))

 
Valeriy Yastremskiy:

Multiply by 10 to the right degree until you get integers. At first I struggled too, but now I even convert the price to an integer and work with integers or rounding to integers. I have fewer errors, better readability.

In general, as one programmer said after receiving summaries from the Exchange, I do not understand why numbers of a certain digit capacity are represented by real numbers, rather than integers. This is not logically correct. ))))

Thanks, I'll use int in the old way and then divide by double. ;)

 
Nauris Zukas:

Thanks, but your example didn't give the right result either. In printf you can see that no normalisation takes place:

Sorry, I should have checked it. And here we have "I wanted to do it right, but it turned out to be the same as always".

Result

2020.05.29 14:26:33.855 !00 (GBPUSD.m,M15)       i: 1.0 step: 0.2
2020.05.29 14:26:33.855 !00 (GBPUSD.m,M15)       i: 1.2 step: 0.2
2020.05.29 14:26:33.855 !00 (GBPUSD.m,M15)       i: 1.4 step: 0.2
2020.05.29 14:26:33.855 !00 (GBPUSD.m,M15)       i: 1.6 step: 0.2
2020.05.29 14:26:33.855 !00 (GBPUSD.m,M15)       i: 1.8 step: 0.2
2020.05.29 14:26:33.855 !00 (GBPUSD.m,M15)       i: 2.0 step: 0.2
2020.05.29 14:26:33.855 !00 (GBPUSD.m,M15)       i: 2.2 step: 0.2
2020.05.29 14:26:33.855 !00 (GBPUSD.m,M15)       i: 2.4 step: 0.2
2020.05.29 14:26:33.855 !00 (GBPUSD.m,M15)       i: 2.6 step: 0.2
2020.05.29 14:26:33.855 !00 (GBPUSD.m,M15)       i: 2.8 step: 0.2
2020.05.29 14:26:33.855 !00 (GBPUSD.m,M15)       MaxPass: 10 count: 10 i: 3.0

Correct code

/********************Script program start function*******************/
double expr1=1.0;
double expr2=2.8;
double step=0.2;
double i;
int count=0;
void OnStart()
 {
  for(i=expr1; i<=expr2; i=NormalizeDouble(i+step, 1))
   {
    count++;
    Print(" i: ", DoubleToString(i, 1), " step: ", DoubleToString(step, 1));
   }
  int MaxPass=(int)NormalizeDouble(((expr2-expr1)/step), 0)+1;
  Print(" MaxPass: ", MaxPass, " count: ", count, " i: ", i);
 }/*******************************************************************/
 
Alexey Viktorov:

Correct code

Be careful not to put step=0.04 ;)

 
Andrey Khatimlianskii:

Be careful not to put step=0.04 ;)

This is only advice for those who don't know how to use their brains.)))

 
Alexey Viktorov:

Sorry, I should have checked. It's just, "I meant well, but it's always the same."

Result

Correct code.

i=NormalizeDouble(i+step, 1)

Ah, thank you! I didn't think of it myself. That's just the kind of thing that helps us newbies in programming! ;)

 
I'm looking formethods to sort an array of structures. Does anyone have any working variants ?
 
Vladimir Pastushak:
I'm looking formethods to sort an array of structures. Does anyone have any working variants ?

https://www.mql5.com/ru/forum/170952/page134#comment_11532251

Особенности языка mql5, тонкости и приёмы работы
Особенности языка mql5, тонкости и приёмы работы
  • 2019.04.19
  • www.mql5.com
В данной теме будут обсуждаться недокументированные приёмы работы с языком mql5, примеры решения тех, или иных задач...
 
fxsaber:

https://www.mql5.com/ru/forum/170952/page134#comment_11532251

It doesn't work...