Decimal counting in nodes

Asked

Viewed 96 times

1

I’m developing a system, where I have to start counting at 1:00 and go all the way to a number that will be chosen in the database.

What I want to know is how I can make a rising counter in nodes, which rises from one hundredth to one hundredth, or 1.01 ; 1.02 etc... Until I reach the value I desire.

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

Note: It does not involve thousandths of second or anything related to time but yes, the higher the number rising even faster it will continue to rise.

How can I do this in nodejs?

Thank you.

  • Can you describe the increment model with a mathematical formula? Or describe 1.01 > 1.02 > ... and then? 1.03 or 1.035, or 1.04?

  • 1

    Gonçalo you saw my question? ^

  • Sorry Sergio, I had no internet access only today can see. What I want does not have any mathematical "formula", but the most similar one is for example: from 1.01 to 2, take 30 seconds. from 2 to 3 take 25 seconds... But the count is 1.01 to 0.01 of 0.01

  • 1

    Okay, is that something you’re looking for? https://jsfiddle.net/85rLudgo/

  • 1

    Gonçalo, you’ve seen my jsFiddle? ^

  • Yes that’s right! Sorry for the delay, but I was waiting for a friend of mine to analyze himself. It is he who is helping me in the project.

  • If there is an answer that solves the problem you can mark as accepted.

Show 2 more comments

2 answers

2

You can implement a "for" in your code where the incremental condition is a sequence modified by the current value of the variable being 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, decreasing from the initial value and summing 0.01.

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 equal them at the end of the loop, as described by the code.

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

2


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

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

It goes from there, to avoid the accumulated mistakes, proposed:

  • integer count;
  • sequence number calculated from the count: count/100
for (var i = min; i < max; i++) {
      var seq = i/100;
      ...
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.