Counter-Up icon indicating copy to clipboard operation
Counter-Up copied to clipboard

24 counts up to 23 and previously posted solutions don't work.

Open temkaes opened this issue 5 years ago • 2 comments

24.0 works, 23 works and 25 works but 24 gets stuck at 23. Interestingly, the same issue can be replicated at 3, 6, 12, 24, 48 and 96...

I tried a few of the earlier posted solutions but still doesn't work.

temkaes avatar Jun 04 '20 08:06 temkaes

The problem is that first num is divided by divisions and only then multiplied by i. Javascript calculates with floats and thus rounds in sometimes unexpected ways. Change the order (first multiply, then divide) and it's gonna be allright. (Or round the result before parseInting.)

fastcatch avatar Oct 19 '20 09:10 fastcatch

My fix...

               // For first iteration, don't divide.
                var auxNum = i == divisions ? num : num / divisions * i;

then...

                // Preserve as int if input was int
                var newNum = parseInt(auxNum);

                // Preserve float if input was float
                if (isFloat) {
                    newNum = parseFloat(auxNum).toFixed(decimalPlaces);
                }

gabrieljenik avatar Jun 11 '21 13:06 gabrieljenik