Socket
Socket
Sign inDemoInstall

add

Package Overview
Dependencies
Maintainers
2
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

add - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

Readme.md

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

61

package.json
{
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc