Created
December 4, 2018 13:24
-
-
Save charlesBochet/57e8d7ad7783bf421989bda8d8c4ad53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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