Features of the mql5 language, subtleties and tricks - page 158

 

There is a matrix, filled with both large, about 10e60, and small, like 1e-40 values. And there is a code like this

      for(k=-16;k<=16;++k)
      {
        double Tmp1=(double)(DoubleToString(Matrix[i][j],k));
        double Tmp2=(double)((string)Matrix[i][j]);
        if(Tmp1==Tmp2)
          break;
      }
In about 27% of cases k=-15. In other cases k=-16. Hence a question: is there a way to replace(string)Matrix[i][j] operation with a stricter conversion function? Or maybe there's a function that cuts off some of the decimal places? NormalizeDouble is not right, it will truncate 1.12345678e-40 into 0. I want it to truncate after the decimal point. Or conversion to (string) has its own life and can not be expressed through functions? Thank you.
 
traveller00:

Modulus of Difference.

 
fxsaber:

The modulus of difference.

Could you be a little more specific? Maybe with a code example? Difference between what and what?

The point is that such double conversion(double)((string)Matrix[i][j]);; will truncate an uncontrollable number of characters after the decimal point. I would like to keep this truncation on the one hand = be able to repeat it, and on the other hand this number of characters is controlled.

 
traveller00:

Could you be a little more specific? Maybe with a code example? Differences between what and what?

if (MathAbs(Value1 - Value2) < Epsilon)
 

Ah, I see your point. No, the question is a bit different. Comparison here is rather just as an example to show that we cannot replace conversion via (double)((string)Matrix[i][j]); with conversion via DoubleToString. In fact, the task at hand is not a comparison. It is rather a certain reset of accuracy through such double conversion. On one hand I want to be able to repeat clipping via (string), on the other hand I want to be able to control the clipping accuracy like DoubleToString does.

And as a side question, could it be that these conversions are done differently and live their separate and unrelated lives?

 
traveller00:

Ah, I see your point. No, the question is a bit different. Comparison here is rather just as an example to show that we cannot replace conversion via (double)((string)Matrix[i][j]); with conversion via DoubleToString. In fact, the task at hand is not a comparison. It is rather a certain reset of accuracy through such double conversion. On one hand I want to be able to repeat clipping via (string), on the other hand I want to be able to control the clipping accuracy like DoubleToString does.

And as a side note, I wonder if these conversions are done differently and live their separate and unrelated lives.

I don't understand why NormalizeDouble andDoubleToString don't satisfy you, they have any precision you want, or you want some precision, but you don't know what it is?

 

I agree that the task is not entirely transparent. Because I'm trying to put together some sort of crutch for the tests.

Say there is a number 1.0123456789e-50. I need to round it with some precision so that:

1. By default, it works as a double conversion via(string).

2. the accuracy can be controlled if desired.

HenceNormalizeDouble is not suitable, it will simply null this number. And DoubleToString does not allow to repeat the result from step 1.

The problem grows from the next question. As I wrote above, there is a matrix. The inverse of this matrix is calculated. Sometimes (perhaps due to errors due to accuracy limitations of double), the matrix turns out to be degenerate and the inverse cannot be calculated for it. But if you cut the precision a bit, you will still be able to calculate the inverse. That is why I wanted to run some tests and get some statistics to what extent the result is similar to the true one. Double conversion via (string) solves the problem in most cases. But sometimes it's not enough and I'd like to be able to control how much precision is cut. And DoubleToString, even with a precision of -16 to 16, does not solve the problem in most cases.

 

Can you tell me please, pls.

In indicator the order of series, e.g. close[], is set by ArraySetAsSeries() once or in some other way ?

Is it done in OnCalculate() or in OnInit() ?

I've encountered a confusing situation:

Order in close[], set by AS_SERIES on entry on first tick, on next tick spontaneously changes to normal, i.e. !AS_SERIES.

I haven't found any reason to do this in the code.

 
Yurixx:

Can you tell me please, pls.

In indicator the order of series, e.g. close[], is set by ArraySetAsSeries() once or in some other way ?

Is it done in OnCalculate() or in OnInit() ?

I've encountered a confusing situation:

Order in close[], set by AS_SERIES on entry on first tick, on next tick spontaneously changes to normal, i.e. !AS_SERIES.

I haven't found the reason for that in the code.

And you will not see the close[] array in OnInit(). And none of other predefined OnCalculate() parameters.

Hence conclusion - only in OnCalculate().

 
Artyom Trishkin:

And in OnInit() you will not see close[] array. And none of the other predefined OnCalculate() parameters.

Hence conclusion - only in OnCalculate().

I came to the same conclusion. )))

It remains to be seen whether setting this order once is enough or if it should be done on every tick.

Reason: