I've created a script to divide numbers with the chosen number of decimal places. Everything works fine unless I set a huge number of decimal places. The file run via Node.js gives up when the decimal numbers array length reaches ~2400, Chrome gives up on ~1900.
I simplified my code and the script sample below throws Maximum call stack size exceeded
RangeError up on ~20000
decimal numbers array length. I thought that this Error throws when the loop or recursive function is called endlessly, but in my case there is a countable number of iterations. It may be a huge number but my module is intended to do the math operations on big numbers.
Why does it happen and Can I avoid this RangeError to occur?
var decimals = [];
var max = 20000;
recurse();
function recurse() {
decimals.push(Math.floor(Math.random()*10));
if(decimals.length === max) return;
recurse();
}
I've created a script to divide numbers with the chosen number of decimal places. Everything works fine unless I set a huge number of decimal places. The file run via Node.js gives up when the decimal numbers array length reaches ~2400, Chrome gives up on ~1900.
I simplified my code and the script sample below throws Maximum call stack size exceeded
RangeError up on ~20000
decimal numbers array length. I thought that this Error throws when the loop or recursive function is called endlessly, but in my case there is a countable number of iterations. It may be a huge number but my module is intended to do the math operations on big numbers.
Why does it happen and Can I avoid this RangeError to occur?
var decimals = [];
var max = 20000;
recurse();
function recurse() {
decimals.push(Math.floor(Math.random()*10));
if(decimals.length === max) return;
recurse();
}
Share
Improve this question
edited Jun 6, 2018 at 1:05
Matus Dubrava
14.5k3 gold badges44 silver badges60 bronze badges
asked Jun 5, 2018 at 15:43
PawełPaweł
4,5366 gold badges25 silver badges42 bronze badges
2
-
2
If this isn't an exercise, it would make more sense as a non-recursive function with a
for
loop. Then you won't need any outside variables – MCMastery Commented Jun 5, 2018 at 15:50 - @Pawel, I think you will find this Q&A helpful stackoverflow./a/43596323/633183 – Mulan Commented Jun 5, 2018 at 16:12
3 Answers
Reset to default 4No, there is a limit to how many function calls you can place on to the call stack so even if your recursive function has a reachable base case, it will still throw the error if the number of recursive calls is too big.
One workaround would be to use technique called trampoline
where you don't perform recursive call directly inside of the function, but insted return a new function that is then executed in a loop until the base case is reached.
Using this technique, your function can perform as many recursive calls as you want because you are not placing more of those function calls to the call stack at the same time, so it will not overflow.
var decimals = [];
var max = 20000;
function _recurse(){
decimals.push(Math.floor(Math.random()*10));
if(decimals.length === max) return;
return () => _recurse();
}
const trampoline = fn => (...args) => {
let res = fn(...args);
while (typeof res === 'function') { res = res(); }
return res;
}
const recurse = trampoline(_recurse);
recurse()
console.log(decimals);
Note that your problem can be solved without the use of recursion in a much simpler way, using loops. For example:
function createRandomSequence(amount) {
const decimals = [];
for (let i = 0; i < amount; i++) {
decimals.push(Math.floor(Math.random()*10));
}
return decimals;
}
console.log(createRandomSequence(10));
Just use a regular loop, or something like:
const random = length => Array.from({ length }, () => Math.floor(Math.random() * 10));
const decimals = random(20000);
Concerning the call stack: see this web page
If it's 20,000 (pseudo random) decimals you're after, you can also use something like:
var maxDecimals = 20000;
var decimals = Array.from({length: maxDecimals})
.map(v => Math.floor(Math.random()*10));
console.log(decimals);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745201266a4616337.html
评论列表(0条)