Published
Edited
May 24, 2022
1 fork
15 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const D = Decimal.clone({precision: 20});
const one = D(1), four = D(4);
let k = 1, y0, y1 = one;
do {
y0 = y1;
if (k & 2) y1 = y0.plus(one.div(k += 2));
else y1 = y0.minus(one.div(k += 2));
yield four.times(y1).toFixed(4);
} while (k < 100000);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function arctan(x) {
const x2 = x.times(x);
let k = 1, y0, y1 = x;
do {
y0 = y1;
if (k & 2) y1 = y0.plus((x = x.times(x2)).div(k += 2));
else y1 = y0.minus((x = x.times(x2)).div(k += 2));
} while (!y1.equals(y0));
return y1;
}
Insert cell
{
const D = Decimal.clone({precision: 14});
const one = D(1), four = D(4);
const a = four.times(arctan(one.div(5)));
const b = arctan(one.div(239));
const pi = four.times(a.minus(b));
return pi.toFixed(10);
}
Insert cell
Insert cell
digits = Generators.observe(notify => {
const worker = new Worker(script);
worker.addEventListener("message", ({data}) => notify(data));
return () => worker.terminate();
})
Insert cell
Insert cell
script = {
const blob = new Blob([`
importScripts("${await require.resolve("decimal.js-light@2/decimal.min.js")}");

const precision = 1000;
const D = Decimal.clone({precision: precision + 4});
const one = D(1), four = D(4);

function* arctan(x) {
const x2 = x.times(x);
let k = 1, y = x;
while (true) {
yield y = y.minus((x = x.times(x2)).div(k += 2));
yield y = y.plus((x = x.times(x2)).div(k += 2));
}
}

function* pi() {
const a = arctan(one.div(5));
const b = arctan(one.div(239));
let y0, y1 = one;
do yield y0 = y1, y1 = four.times(four.times(a.next().value).minus(b.next().value));
while (!y1.equals(y0));
}

for (const p of pi()) {
postMessage(p.toFixed(precision));
}

close();
`], {type: "text/javascript"});
const script = URL.createObjectURL(blob);
invalidation.then(() => URL.revokeObjectURL(script));
return script;
}
Insert cell
Insert cell
Insert cell
Decimal = require("decimal.js-light@2/decimal.min.js")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more