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

 
Andrey Khatimlianskii:

I don't need to.

You've got a big mouth, don't you...?

Here's an array of 10000 elements, completely randomised, randomised before each sorting cycle. Total time (ms) for 1000 repetitions of each method.

Built-in ArraySort, my method written a couple of days ago (before that I didn't deal with sorting algorithms at all), and 6 best methods from your library (the rest were much worse), while I removed everything related to graphics from there...

#include <Sort\GSort.mqh>
#define    ttt                 template<typename T>
#define    test(M,S,EX)        {uint mss=GetTickCount();int nn=(int)pow(10,M);for(int tst=0;tst<nn&&!_StopFlag;tst++){EX;} \
                                printf("%s: loops=%i ms=%u",S,nn,GetTickCount()-mss);}


ttt void a_rand(T&ar[]){for(int i=0;i<ArraySize(ar);i++)ar[i]=T(rand()*rand());}

ttt int TreePop(T&t[],T&ar[],int i=0,int b=0)
 {
  if(t[b+1]>=0)i=TreePop(t,ar,i,(int)t[b+1]);
  ar[i++]=t[b];
  if(t[b+2]>=0)i=TreePop(t,ar,i,(int)t[b+2]);
  return i;
 }

ttt void TreeSort(T&ar[])
 {
  int sz=ArrayRange(ar,0);
  T t[];ArrayResize(t,sz*3);ArrayInitialize(t,-1);
  for(int i=0;i<ArraySize(ar);i++){t[i*3]=ar[i];
    if(!i)continue;
    int b=0;
    while(1)
      if(ar[i]<=t[b])
        if(t[b+1]>=0)b=(int)t[b+1];
        else{t[b+1]=i*3;break;}
      else
        if(t[b+2]>=0)b=(int)t[b+2];
        else{t[b+2]=i*3;break;}}
  TreePop(t,ar);
 }


void OnStart(){
  double ar[];
  int N=10000,k=3;
  ArrayResize(ar,N);

  test(k,"ArraySort(MQL5)",a_rand(ar);ArraySort(ar))
  test(k,"TreeSort(AntFX)",a_rand(ar);TreeSort(ar))
  test(k,"Merge(GSort)",a_rand(ar);GMergesort(ar,0,N-1))
  test(k,"QTernaryLL(GSort)",a_rand(ar);GQSortTernaryLL(ar,0,N-1))
  test(k,"QSortLL(GSort)",a_rand(ar);GQSortTernaryLL(ar,0,N-1))
  test(k,"QSort(GSort)",a_rand(ar);GQSortTernaryLR(ar,0,N-1))
  test(k,"QTernaryLR(GSort)",a_rand(ar);GQSortTernaryLR(ar,0,N-1))
  test(k,"Comb(GSort)",a_rand(ar);GComb(ar))
}
 
Ilya Malev:

You've got a big mouth, haven't you...?

Here's an array of 10000 elements, completely randomised, randomised before each sorting cycle. Total time (ms) for 1000 repetitions of each method.

Built-in ArraySort, my method written a couple of days ago (before that I wasn't doing sorting algorithms at all), and 6 best methods from your library (the rest were much worse), while I removed everything related to graphics from there...

Judging by the video from the article, the fastest ones are Count, LSD and MSD.

 
Andrey Khatimlianskii:

Judging from the video in the article, the fastest ones are Count, LSD and MSD.

I never waited for these passes to be completed.

 
Alexandr Sokolov:

I think I am not the only one who had a rare situation where I needed to get the number of decimal places, and Digits() function works only with quotes, and besides there is no information about it anywhere (at least at the time of writing this post I have not found it before, so I want to show what solution I found).


As it turned out, the essence of the banal simple, but still has one drawback - this function does not recognize zeros, if after them there are no other digits. For example, the function will return 2 when followed by 0.01, but when followed by 0.0000 it will return 0 (i.e. it can't see four zeros). So, consider this shortcoming in your developments.


Code in MQL4


The code at MQL5

The MQL5 code had to be slightly improved, as apparently in MQL5 variables of double type are automatically assigned 0 at the end, no matter whether the variable is an integer or not. And for this reason, the function has never returned 0.

If this topic is relevant, here is my example:

int kol_Z(double zzz) { // calculate the number of decimal places

string a, d;
int b, c;
a=StringFormat("%g", zzz);
b=StringFind(a,".",0);
c=StringLen(a);
if (b==-1) return(0);
d=StringSubstr(a,b+1);
return(StringLen(d));
}
 
NomadSoul:

if the topic is relevant, here's my take on it:

Example:

void OnStart()
{
    Print(kol_Z(1.001234));
}

Result: 5 and it should be 6

 
A100:

Example:

Result: 5, which should be 6.

the number of decimal places?". How can you, when you give a computer a number that it is required to truncate to the length of the mantissa it has, then ask it where the end of that infinite periodic fraction was?

0.000110011001100110011(0011) is a decimal number 0.1 in binary representation. The periodic part of the infinite fraction is in brackets. So, what should the computer answer, if it stores only the first 52 significant digits of the infinite number in double?

No one is surprised that the very short ternary number 0.1 in decimal representation (0.33333...) has an infinite number of significant digits after the decimal point. Binary 0.1 is equal to decimal 0.5 with a finite number of digits by happy accident, found and reduced a common divisor in the two bases of notation systems 2 and 10, it is 2. Degrees of half in both representations are also good: 0.5 => 0.1; 0.05 => 0.01 ; 0.025 => 0.001 ; 0.0125 => 0.0001. But as soon as 5 appears in the denominator of a fraction, that's it, there are a lot of significant digits.

The number of significant digits in the fractional part is related to the number of characters used in the representation, not only to the value of the number.

Самый простой способ посчитать количество знаков после запятой?
Самый простой способ посчитать количество знаков после запятой?
  • 2021.01.18
  • www.mql5.com
Есть переменная. Например: double а=0.02; Нужно написать функцию, которая считала бы количество знаков после запятой...
 

1

I've run it three times. It's breathing down the leader's neck... If anyone needs it, the function here ishttps://www.mql5.com/ru/code/904, the function name is SortHoareUp.

 
Dmitry Fedoseev:

I've run it three times. It's breathing down the leader's neck... If anyone needs, the function here ishttps://www.mql5.com/ru/code/904, the function name is SortHoareUp.

There is alsoMathQuickSort() from the library (#include <Math\Stat\Math.mqh>). I remember there was an article where they wrote that it's very-very fast library).

I haven't measured the speed, I use it mainly because it allows me to save array of indexes of source array.

 
Vladimir:

the number of decimal places?". How can you, when you give a computer a number that it has to truncate to the length of the mantissa it has, then ask it where the end of that infinite periodic fraction was?

0.000110011001100110011(0011) is a decimal number 0.1 in binary representation. The periodic part of the infinite fraction is in brackets. So, what should the computer answer, if it stores only the first 52 significant digits of the infinite number in double?

No one is surprised that the very short ternary number 0.1 in decimal representation (0.33333...) has an infinite number of significant digits after the decimal point. Binary 0.1 is equal to decimal 0.5 with a finite number of digits by happy accident, found and reduced a common divisor in the two bases of notation systems 2 and 10, it is 2. Degrees of half in both representations are also good: 0.5 => 0.1; 0.05 => 0.01 ; 0.025 => 0.001 ; 0.0125 => 0.0001. But as soon as 5 appears in the denominator of a fraction, that's it, there are a lot of significant digits.

The number of significant digits in the fractional part is related to the number of digits used in the representation, and not only to the value of the number.

It would be a good idea to first define the conditions of the problem to be solved. If we are talking about some kind of theoretical study of number representation in the computer, we need to further clarify the purpose of this study.

If the question is about which integer to supply as the second argument to the NormalizeDouble() function, you should use the same function to find the answer. This will be the minimum integer between 0 and 8, for which the normalized number will be equal to the source number. If no such integer can be found, the source number is incorrect. Here is an example ofcode where the number of digits is counted for the minimum volume step.

Reason: