Skip to content

Instantly share code, notes, and snippets.

@charlesBochet
Created December 4, 2018 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlesBochet/57e8d7ad7783bf421989bda8d8c4ad53 to your computer and use it in GitHub Desktop.
Save charlesBochet/57e8d7ad7783bf421989bda8d8c4ad53 to your computer and use it in GitHub Desktop.
var precision = 300
Big.DP = precision
function computePi() {
var bigOne = new Big(1)
document.getElementById('pi').innerHTML =
((atan(bigOne.div(5)).times(4)).minus(atan(bigOne.div(239)))).times(4)
.toPrecision(precision)
}
function atan(x) {
var preciseX = new Big(x);
var result = preciseX;
var xSquared = preciseX.times(preciseX);
var term = preciseX;
var divisor = new Big(1);
while (term.times(new Big(10).pow(precision)) > 1) {
divisor = divisor.plus(2);
term = term.times(xSquared);
result = result.minus(term.div(divisor));
divisor = divisor.plus(2);
term = term.times(xSquared);
result = result.plus(term.div(divisor));
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment