Get the number of decimal places of any numbers (not just quotes) bypassing Digits() in MQL4 and MQL5 - page 12

 
Ilya Malev:

This is probably the best solution

it is not correct to compare doubles on equality, moreover, the task is to be exact to the sign

Look at the stdlib.mq4 file in MT4, there was an example of correct double comparison

CompareDoubles()

SZZ: here is an article on the subjecthttps://www.mql5.com/ru/articles/1561

Особенности работы с числами типа double в MQL4
Особенности работы с числами типа double в MQL4
  • www.mql5.com
При программировании на языке MQL4 у новичков порой возникают ситуации, когда результаты некоторых математических вычислений отличаются от ожидаемых. При этом программа компилируется и работает, но не так, как нужно. Они начинают разбираться в программе, находят новые "ошибки" в языке, реализации функций и т.п. В большинстве случаев последующий...
 
Aliaksandr Hryshyn:
I wonder how you'll do logarithms, exponentiation with non integer numbers, trigonometry, .... use third party libraries, indicators... They all have errors!

If you need super-mega precision, you can use special external libraries like gnu mp

Otherwise, if you need guaranteed accuracy (when at each operation error is calculated), you should use all sorts of "interval libs".

if even that is not enough - you can use polynomials instead of digits

but I've never seen such cases, when such circuits are really needed from MT:-)

there's a double for the eye :-)

 
Igor Makanu:

it is not correct to compare doubles on equality, moreover, the task is to be exact to a sign

Look at the stdlib.mq4 file in MT4, there was an example of correct double comparison

CompareDoubles()

Well, well, and the runtime with CompareDoubles soars by a factor of 2. Then it's better this way:

int dtd(double f)
 {
  f/=0.0000001;
 
  if(int(fmod(f,10000000))==0)
   {
    return 0;
   }
  if(int(fmod(f,1000000))==0)
   {
    return 1;
   }
  if(int(fmod(f,100000))==0)
   {
    return 2;
   }
  if(int(fmod(f,10000))==0)
   {
    return 3;
   }
  if(int(fmod(f,1000))==0)
   {
    return 4;
   }
  if(int(fmod(f,100))==0)
   {
    return 5;
   }
  if(int(fmod(f,10))==0)
   {
    return 6;
   }
  return 7;
 }
 
Ilya Malev:

Well, well, and the execution time with CompareDoubles soars by a factor of two. You'd better do it this way:

We are talking about correct calculation, I can't check your code now, but it's not difficult, you need to compare your example with an example using DoubleToStr() in the loop, if there is no difference, then everything is correct

ZS: I tried with fmod() I think I posted an example, it does not work correctly, like at 0.07 should check - immediately a bug popped up

 
Igor Makanu:

We are talking about correct calculation, I cannot check your code now, but it's not hard, you need to compare your example with the example using DoubleToStr() in the loop, if there is no difference, then everything is correct

I have already checked it and calculated the speed and it is the best way. Unless you need to look for more than 7 characters, of course.

S.s. 0.7 and 0.07 and 500000000.0001, etc. all calculated correctly.

s.s. But for 500000000.9991, it handles worse. But it hardly matters in forex practice.

 
Ilya Malev:

But in forex practice this is hardly important.

it depends on the problem. if it is for trade orders, then this problem is meaningless - prices are normalized to Digits, while lots are relative to the minimum lot

If the problem is with mathematical modeling, it may be useful.

 
Igor Makanu:

it depends on the problem, if it is for trading orders, then this task is meaningless at all - prices are normalized to Digits, and lots are relative to the minimum lot

If the task is with mathematical modeling, it may be useful.

Of course, not to write neural networks =))) and say, it is convenient to store prices/lots/money as a 4-byte integer with decimal value, reducing data size by 2 times.

 
Igor Makanu:

it is not correct to compare doubles on equality, moreover, the task is to be exact to a sign

Look at the stdlib.mq4 file in MT4, there was an example of correct double comparison

CompareDoubles()

SZZ: here was an article on the subjecthttps://www.mql5.com/ru/articles/1561

So the task is to determine by how many digits x is normalized. So it's OK. Unless you were too lazy to write such a ladder.

 
Dmitry Fedoseev:

Unless you were too lazy to write such a ladder.

You can do it without a ladder, but it takes 10% longer.

int dtd2(double f)
 {
  f/=0.0000001;
  int d = 0, i = 10000000;
 
  while( d < 7 && int( fmod( f, i ) ) > 0 )
   {
    i /= 10;
    d ++ ;
   }

  return d ;
 }
 
Ilya Malev:

You can do it without a ladder, but it takes 10% longer.

This one: f/=0.0000001; is questionable.