Increasing decimal count in nodejs

1

I'm developing a system, where I have to start counting at 1.00 and then go through there a number that will be chosen in the database.

What I want to know is how can I make an increasing count in nodejs, which rises from hundredth to hundredth or 1.01; 1.02 etc ... Until I get the value I want.

However, the higher the number is, the faster it will go up, then it will rise faster and faster.

NOTE: It does not involve thousandths of a second or anything related to time but rather, the larger the number going up the faster it will continue to rise.

How can I do this on nodejs?

Thank you.

    
asked by anonymous 05.07.2016 / 05:37

2 answers

2

What I'm going to say is probably an irrelevant comment ...

The number 0.01 is treacherous: it has no exact representation in normal binary notation (infinite periodic tithing). You experience something like for(var i = 1; i < 1000; i+=0.01){imprimir i} and accumulated errors are easily visible.

Go from there, to avoid the accumulated errors, proposed:

  • counting with integers;
  • number of the sequence calculated from the count: count / 100
for (var i = min; i < max; i++) {
      var seq = i/100;
      ...
}
    
05.07.2016 / 12:49
2

You can implement a "for" in your code where the incremental condition is a sequence modified by the current value of the variable to be incremented, as follows:

var min_inicial = 1.0    

for(var min = 1.0; min < max; min += (min - min_inicial + 0.01)) 
{
   //Garante que min não será superior a max, 
   //  considerando o método de incremento utilizado.
   if(min > max) min = max;

   //Código que utilizará a sequencia aqui...
}

Where: - "max" is the limit value obtained from the database. - at each iteration of "for" the variable min will receive its own value, decremented from the initial value and added to 0.01.

In this way the variable would receive the following values after n interactions:

n is 1 = > min = 1.0 + (1.0 - 1.0 + 0.01) = > min = 1.01

n is 2 = > min = 1.01 + (1.01 - 1.0 + 0.01) = > min = 1.03

n is 3 = > min = 1.03 + (1.03 - 1.0 + 0.01) = > min = 1.07

It is necessary to be careful with this algorithm because the final value of min can be greater than max, in this case it is necessary to match them to the end of the loop, as described in the code.

It's not the most beautiful implementation, but I think it solves your problem.

    
05.07.2016 / 06:02