With regard to tail recursion, what is wrong with me? why is it that when the input is large, it will still overflow the stack?

as mentioned in the question, I use recursion to calculate the approximate value of PI: PI/4 = 1-1 PI 3 + 1 hand 7 + 1 hand 9 +;
code is as follows:

let calPI = (n, sum = 1)=> {
    if(n < 3) 
        return sum*4;    
    n%4 === 1 ? sum += 1/n : sum -= 1/n;
    return calPI(n-2, sum);
}
console.log(calPI(99999));

calPI (9999) there is no stack overflow. Am I writing something wrong?

Mar.23,2021

so far (July 3, 2018), other major browsers, such as Google (including v8, node.js) and Firefox, have not implemented TCO (Tail call optimization) in the javascript engine except Safari.

you can use other languages that implement TCO to test algorithms, such as C language.

references

  1. Tail call optimization in ECMAScript 6, http://2ality.com/2015/06/tai.
  2. es6, https://kangax.github.io/comp.

V8 used to have tail recursive optimization, but now
has been deleted because tail recursive optimization destroys the call stack information of the function

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e47d05-43215.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e47d05-43215.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?