MCPcopy Create free account
hub / github.com/arguiot/TheoremJS / pi

Method pi

__test__/theorem.js:1230–1261  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1228 return new BigNumber(out)
1229 }
1230 get pi() {
1231 const digits = 15
1232 const Decimal = BigNumber.clone({ DECIMAL_PLACES: digits })
1233 function arctan(x) {
1234 var y = x;
1235 var yPrev = NaN;
1236 var x2 = x.times(x);
1237 var num = x;
1238 var sign = -1;
1239
1240 for (var k = 3; !y.eq(yPrev); k += 2) {
1241 num = num.times(x2);
1242
1243 yPrev = y;
1244 y = (sign > 0) ? y.plus(num.div(k)) : y.minus(num.div(k));
1245 sign = -sign;
1246 }
1247
1248 return y;
1249 }
1250
1251 // Machin: Pi / 4 = 4 * arctan(1 / 5) - arctan(1 / 239)
1252 // http://milan.milanovic.org/math/english/pi/machin.html
1253
1254 // we calculate pi with a few decimal places extra to prevent round off issues
1255 var DecimalPlus = BigNumber.clone({ DECIMAL_PLACES: digits + 4 })
1256 var pi4th = new DecimalPlus(4).times(arctan(new DecimalPlus(1).div(5)))
1257 .minus(arctan(new DecimalPlus(1).div(239)));
1258
1259 // the final pi has the requested number of decimals
1260 return new Decimal(4).times(new Decimal(pi4th))
1261 }
1262 primeFactors(n) {
1263 n = new BigNumber(n).toNumber()
1264 if (n < 2) {

Callers

nothing calls this directly

Calls 5

arctanFunction · 0.85
cloneMethod · 0.80
minusMethod · 0.80
timesMethod · 0.80
divMethod · 0.80

Tested by

no test coverage detected