I know this question has been asked before but I couldn't exactly find the answer. I'm trying to make a function that accurately calculates pi to x places. Here's my code so far:
function odd(num) { return num % 2;}
var pi = 3;
var x=2;
for (var acc = 1; acc < 30000; acc++) {
if (Odd(acc)) {
pi = pi + (4/(x * (x+1) * (x+2)));
} else {
pi = pi - (4/(x * (x+1) * (x+2)));
}
x=x+2;
}
console.log(pi);
This works, but how do I specify the length while also making sure it stays accurate?