Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewBarfield/f9535992ecaff4d18802 to your computer and use it in GitHub Desktop.
Save AndrewBarfield/f9535992ecaff4d18802 to your computer and use it in GitHub Desktop.
Web Worker Script for "Calculating Pi in a Web Worker"
This is the Web Worker for<br/>&quot;Calculating Pi in a Web Worker&quot;.
<!-- To see more, go here: http://codepen.io/AndrewBarfield/pen/VYKwJO -->
/*
This script was adapted from Ken Ward's Java Script Tutorial located at:
http://www.trans4mind.com/personal_development/JavaScript/longnumPiMachin.htm
*/
mess = "";
Base = Math.pow(10, 11);
cellSize = Math.floor(Math.log(Base) / Math.LN10);
function makeArray(n, aX, Integer) {
var i = 0;
for (i = 1; i < n; i++)
aX[i] = null;
aX[0] = Integer;
}
function isEmpty(aX) {
var empty = true
for (i = 0; i < aX.length; i++)
if (aX[i]) {
empty = false;
break;
}
return empty;
}
function Add(n, aX, aY) {
carry = 0
for (i = n - 1; i >= 0; i--) {
aX[i] += Number(aY[i]) + Number(carry);
if (aX[i] < Base)
carry = 0;
else {
carry = 1;
aX[i] = Number(aX[i]) - Number(Base);
}
}
}
function Sub(n, aX, aY) {
for (i = n - 1; i >= 0; i--) {
aX[i] -= aY[i];
if (aX[i] < 0) {
if (i > 0) {
aX[i] += Base;
aX[i - 1] --;
}
}
}
}
function Mul(n, aX, iMult) {
carry = 0;
for (i = n - 1; i >= 0; i--) {
prod = (aX[i]) * iMult;
prod += carry;
if (prod >= Base) {
carry = Math.floor(prod / Base);
prod -= (carry * Base);
} else
carry = 0;
aX[i] = prod;
}
}
function Div(n, aX, iDiv, aY) {
carry = 0;
for (i = 0; i < n; i++) {
currVal = Number(aX[i]) + Number(carry * Base);
theDiv = Math.floor(currVal / iDiv);
carry = currVal - theDiv * iDiv;
aY[i] = theDiv;
}
}
function arctan(iAng, n, aX) {
iAng_squared = iAng * iAng;
k = 3;
sign = 0;
makeArray(n, aX, 0);
makeArray(n, aAngle, 1);
Div(n, aAngle, iAng, aAngle);
Add(n, aX, aAngle);
while (!isEmpty(aAngle)) {
Div(n, aAngle, iAng_squared, aAngle);
Div(n, aAngle, k, aDivK);
if (sign)
Add(n, aX, aDivK);
else Sub(n, aX, aDivK);
k += 2;
sign = 1 - sign;
}
mess += "aArctan=" + aArctan + "<br>";
}
// Calculate pi
function calcPI(numDec) {
var ans = "";
t1 = new Date();
numDec = Number(numDec) + 5;
iAng = new Array(10);
coeff = new Array(10);
arrayLength = Math.ceil(1 + numDec / cellSize);
aPI = new Array(arrayLength);
aArctan = new Array(arrayLength);
aAngle = new Array(arrayLength);
aDivK = new Array(arrayLength);
coeff[0] = 4;
coeff[1] = -1;
coeff[2] = 0;
iAng[0] = 5;
iAng[1] = 239;
iAng[2] = 0;
makeArray(arrayLength, aPI, 0);
makeArray(arrayLength, aAngle, 0);
makeArray(arrayLength, aDivK, 0);
for (var i = 0; coeff[i] != 0; i++) {
arctan(iAng[i], arrayLength, aArctan);
Mul(arrayLength, aArctan, Math.abs(coeff[i]));
if (coeff[i] > 0)
Add(arrayLength, aPI, aArctan);
else
Sub(arrayLength, aPI, aArctan);
}
//we have calculated pi/4, so need to finally multiply
Mul(arrayLength, aPI, 4);
//we have now calculated PI, and need to format the answer
//to print it out
sPI = "";
tempPI = "";
//put the figures in the array into the string tempPI
for (i = 0; i < aPI.length; i++) {
aPI[i] = String(aPI[i]);
if (aPI[i].length < cellSize && i != 0) {
while (aPI[i].length < cellSize)
aPI[i] = "0" + aPI[i];
}
tempPI += aPI[i];
}
for (i = 0; i <= numDec; i++) {
if (i == 0)
sPI += tempPI.charAt(i) + ".";
else {
if ((i) % 50 == 0 && i != 0)
sPI += tempPI.charAt(i);
else
if (i % 5 == 0)
sPI += tempPI.charAt(i);
else
sPI += tempPI.charAt(i);
}
}
ans += (sPI);
t2 = new Date();
return ans;
}
self.onmessage = function(e) {
self.postMessage(calcPI(5000));
};
/* latin */
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 400;
src: local('Roboto Regular'), local('Roboto-Regular'), url(http://fonts.gstatic.com/s/roboto/v14/fg2nPs59wPnJ0blURyMU3PesZW2xOQ-xsNqO47m55DA.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215, U+E0FF, U+EFFD, U+F000;
}
body {
margin: 20px;
font: 60px/60px 'Roboto', sans-serif;
color:#fff;
background-color: #593f6b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment