Questions from Beginners MQL5 MT5 MetaTrader 5 - page 154

 
paladin800:
That is, you need to convert 1.33182 to 1.3318? You would simply cut off the 5th digit after the decimal point or round it up to the 4th digit, e.g., you would have 1.3318 or 1.3319 from 1.33186?

My question is whether there is a bug or not. When converting from double to int in my example, the result is one less. Please check it yourself.

   double max=NormalizeDouble(1.33182,4);  
   //double max=1.3318;
   Alert(int(max*10000));
Alert should give out 13318, but it gives out 13317. If not rounded to int, it will give 13318.0 correctly.
 

Tell me if there is a difference in the expressions:

if(bufferclose[1]<=MaxLine(1)-m_open_bar_error*ticksize && bufferopen[1]<=MaxLine(1)-m_open_bar_error*ticksize)

и

if(( bufferclose[1]<=MaxLine(1)-m_open_bar_error*ticksize) && ( bufferopen[1]<=MaxLine(1)-m_open_bar_error*ticksize))
 
sgreen:

Tell me if there is a difference in the expressions:

и

If you have a suspicion that something is not right, make it so:

if(bufferclose[1]<= (  MaxLine(1)-m_open_bar_error*ticksize ) && bufferopen[1]<= (  MaxLine(1)-m_open_bar_error*ticksize ) )
 
Rorschach:

My question is whether there is a bug or not. When converting from double to int in my example, the result is one less. Please check with yourself.

Alert should give out 13318, but gives out 13317. Thus if not to reduce to int, then gives out correctly 13318.0
Alert does not give out an error. The point is that normalization leads number to binary form, which is closest to the requested decimal analogue, while may not coincide with it in more or less (in your case) side. Since what is expressed by finite decimal fraction, not always can be expressed by finite binary . Expression
int(max*10000)

causes discarding of fractional part instead of rounding (i.e. similar to MathFloor() rather than MathRound()), and in your case, the normalized number might well be equal to 13318 exactly, but say 13317,99999999999999997

which, when outputting in fractional form with limited number of digits causes rounding to correct (expected) result, and when fractional part of.... is discarded you see for yourself.

I hope I explained clearly.

 
pronych:
is there an analogue of the Sharp's partial? or how to do it?

there is no analogue. you can embed a piece of definition using #include <partial_class_code.mqh>.

Some people even imitate multiple inheritance in this way. // I do not do this, I got used to do without it. :)

 
paladin800:

If you suspect something is amiss, make it so:

I already have. Just curious.
 
sgreen:
I already did. Just curious.

I see. Returning to your previous question, the essence of the record can be drawn to this comparison:

if (A<=B && C<=D)     // 1
if ((A<=B) && (C<=D)) // 2

There is no difference between these entries. At the same time, I recommend that you bracket mathematical calculations inside if-os:

if (A<=(B1+B2*B3) && C<=(D1-D2*D3))
Распределенные вычисления в сети MQL5 Cloud Network
Распределенные вычисления в сети MQL5 Cloud Network
  • cloud.mql5.com
Заработать деньги, продавая мощности своего компьютера для сети распределенных вычислений MQL5 Cloud Network
 
MetaDriver:
The alert does not produce an error. The point is that normalization leads a number to a binary form, which is closest to the requested decimal analogue, while it may not coincide with it in a larger or smaller (in your case) direction. Since what is expressed by a finite decimal fraction, may not always be expressed by a finite binary . The expression

results in discarding the fractional part rather than rounding (i.e. looks more like MathFloor() than MathRound()), and in your case the normalised number might well be 13317.99999999999999997 rather than 13318 exactly.

which, when outputting in fractional form with limited number of digits causes rounding to correct (expected) result, and when fractional part of.... is discarded you see for yourself.

I hope I've explained it clearly.

Thanks, I see.

Another question, how do I fight this if I cannot do without converting to int?

 
Can you tell me how to take the broker's commission into account when testing an EA?
 
Rorschach:

Thank you, I see.

Another question, how do you deal with this if you can't do without transferring to int?

and you add a couple of epsilons.

DBL_EPSILON

The smallest number for which the condition 1.0+DBL_EPSILON != 1.0 is met


Документация по MQL5: Стандартные константы, перечисления и структуры / Именованные константы / Константы числовых типов
Документация по MQL5: Стандартные константы, перечисления и структуры / Именованные константы / Константы числовых типов
  • www.mql5.com
Стандартные константы, перечисления и структуры / Именованные константы / Константы числовых типов - Документация по MQL5
Reason: