Skip to content

Instantly share code, notes, and snippets.

@charlesBochet
Last active December 4, 2018 13:24
Show Gist options
  • Save charlesBochet/4486419299f823bd900cb72dfc6d508e to your computer and use it in GitHub Desktop.
Save charlesBochet/4486419299f823bd900cb72dfc6d508e to your computer and use it in GitHub Desktop.
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(100)
}
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 != 0) {
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