Skip to content

Instantly share code, notes, and snippets.

@sudojunior
Created March 15, 2019 12:38
Show Gist options
  • Save sudojunior/ca8deae5450b6f497b5182f50ddc3c44 to your computer and use it in GitHub Desktop.
Save sudojunior/ca8deae5450b6f497b5182f50ddc3c44 to your computer and use it in GitHub Desktop.
Versions of calculating PI (where [x] ≒ [PI])
console.clear();
console.log('[Start]');
sqrt = Math.sqrt;
a = n = 1;
g = 1 / sqrt(2); z = 1 / 4; half = 1 / 2;
for(var i = 0; i < 30; i++) {
x = [(a + g) * half, sqrt(a * g)];
val = x[0] - a;
z -= val * val * n;
n += n;
a = x[0]; g = x[1];
}
console.log(a * a / z);
console.log('[End]');
// Failure - 15 point decimal rounding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment