Comparing version 1.0.0 to 2.0.0
138
index.js
@@ -0,6 +1,138 @@ | ||
// The minimum machine rounding error | ||
var Epsilon = Math.pow(2, -53) | ||
, EpsilonReciprocal = (1 / Epsilon) | ||
/// The smallest positive number that can be represented | ||
, Eta = Math.pow(2, -1074) | ||
// limitB is a constant used in the transform function | ||
, limitB = 0.5 * EpsilonReciprocal * Eta | ||
module.exports = add | ||
/** | ||
* S. M. RUMP, T. OGITA AND S. OISHI | ||
* http://www.ti3.tu-harburg.de/paper/rump/RuOgOi07I.pdf | ||
*/ | ||
function add(a, b) { | ||
return a + b | ||
// Page 8 | ||
// x is result, y is error | ||
// third is so the array is allocated for 4 spaces | ||
// it speeds up transform | ||
function fastTwoSum(a, b) { | ||
var x = a + b | ||
, q = x - a | ||
, y = b - q | ||
return [x, y, null] | ||
} | ||
// Page 12 | ||
// p = q + p' | ||
// sigma is a power of 2 greater than or equal to |p| | ||
function extractScalar(sigma, p) { | ||
var q = (sigma + p) - sigma | ||
, pPrime = p - q | ||
return [q, pPrime] | ||
} | ||
// Page 12 | ||
function extractVector(sigma, p) { | ||
var tau = 0.0 | ||
, extracted | ||
, i = 0 | ||
, ii = p.length | ||
, pPrime = new Array(ii) | ||
for(; i<ii; ++i) { | ||
extracted = extractScalar(sigma, p[i]) | ||
pPrime[i] = extracted[1] | ||
tau += extracted[0] | ||
} | ||
return [tau, pPrime] | ||
} | ||
// Finds the immediate power of 2 that is larger than p | ||
//// in a fast way | ||
function nextPowerTwo (p) { | ||
var q = EpsilonReciprocal * p | ||
, L = Math.abs((q + p) - q) | ||
if(L === 0) | ||
return Math.abs(p) | ||
return L | ||
} | ||
// Helper, gets the maximum of the absolute values of an array | ||
function maxAbs(arr) { | ||
var i = 0 | ||
, ii = arr.length | ||
, best = -1 | ||
for(; i<ii; ++i) { | ||
if(Math.abs(arr[i]) > best) { | ||
best = arr[i] | ||
} | ||
} | ||
return best | ||
} | ||
function transform (p) { | ||
var mu = maxAbs(p) | ||
, M | ||
, sigmaPrime | ||
, tPrime | ||
, t | ||
, tau | ||
, sigma | ||
, extracted | ||
, res | ||
// Not part of the original paper, here for optimization | ||
, temp | ||
, bigPow | ||
, limitA | ||
if(mu === 0) { | ||
return [0, 0, p, 0] | ||
} | ||
M = nextPowerTwo(p.length + 2) | ||
bigPow = Math.pow(2, 2 * M) | ||
sigmaPrime = Math.pow(2, M) * nextPowerTwo(mu) | ||
tPrime = 0 | ||
do { | ||
t = tPrime | ||
sigma = sigmaPrime | ||
extracted = extractVector(sigma, p) | ||
tau = extracted[0] | ||
tPrime = t + tau | ||
p = extracted[1] | ||
if(tPrime === 0) { | ||
return transform(p) | ||
} | ||
temp = Epsilon * sigma | ||
sigmaPrime = Math.pow(2, M) * temp | ||
limitA = bigPow * temp | ||
} | ||
while( Math.abs(tPrime) < limitA && sigma > limitB ) | ||
// res already allocated for 4 | ||
res = fastTwoSum(t, tau) | ||
res[2] = p | ||
return res | ||
} | ||
function accSum(p) { | ||
var tfmd = transform(p) | ||
return tfmd[0] + (tfmd[1] + tfmd[2].reduce(function (a,b){return a+b})) | ||
} | ||
module.exports = accSum | ||
module.exports.fastTwoSum = fastTwoSum | ||
module.exports.nextPowerTwo = nextPowerTwo |
{ | ||
"name": "add", | ||
"version": "1.0.0", | ||
"description": "Add two numbers", | ||
"keywords": [], | ||
"author": "Raynos <raynos2@gmail.com>", | ||
"repository": "git://github.com/Raynos/add.git", | ||
"main": "index", | ||
"homepage": "https://github.com/Raynos/add", | ||
"contributors": [{ | ||
"name": "Raynos" | ||
}], | ||
"bugs": { | ||
"url": "https://github.com/Raynos/add/issues", | ||
"email": "raynos2@gmail.com" | ||
"version": "2.0.0", | ||
"description": "A numerically stable algorithm to add numbers accurately", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test.js" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"tape": "~2.1.0" | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/ben-ng/add.git" | ||
}, | ||
"licenses": [{ | ||
"type": "MIT", | ||
"url": "http://github.com/Raynos/add/raw/master/LICENSE" | ||
}], | ||
"scripts": { | ||
"test": "node ./test/index.js" | ||
"keywords": [ | ||
"numerically", | ||
"stable", | ||
"error", | ||
"propagation", | ||
"summation", | ||
"accumulate", | ||
"addition", | ||
"analysis" | ||
], | ||
"author": "Ben Ng <me@benng.me>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/ben-ng/add/issues" | ||
}, | ||
"testling": { | ||
"files": "test/index.js", | ||
"browsers": [ | ||
"ie/8..latest", | ||
"firefox/16..latest", | ||
"firefox/nightly", | ||
"chrome/22..latest", | ||
"chrome/canary", | ||
"opera/12..latest", | ||
"opera/next", | ||
"safari/5.1..latest", | ||
"ipad/6.0..latest", | ||
"iphone/6.0..latest", | ||
"android-browser/4.2..latest" | ||
] | ||
} | ||
"homepage": "https://github.com/ben-ng/add" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
5956
0
131
44
0
5
2