-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrobust-scale.js
50 lines (47 loc) · 862 Bytes
/
robust-scale.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"use strict"
var twoProduct = require("two-product")
var twoSum = require("two-sum")
module.exports = scaleLinearExpansion
function scaleLinearExpansion(e, scale) {
var n = e.length
if(n === 1) {
var ts = twoProduct(e[0], scale)
if(ts[0]) {
return ts
}
return [ ts[1] ]
}
var g = new Array(2 * n)
var q = [0.1, 0.1]
var t = [0.1, 0.1]
var count = 0
twoProduct(e[0], scale, q)
if(q[0]) {
g[count++] = q[0]
}
for(var i=1; i<n; ++i) {
twoProduct(e[i], scale, t)
var pq = q[1]
twoSum(pq, t[0], q)
if(q[0]) {
g[count++] = q[0]
}
var a = t[1]
var b = q[1]
var x = a + b
var bv = x - a
var y = b - bv
q[1] = x
if(y) {
g[count++] = y
}
}
if(q[1]) {
g[count++] = q[1]
}
if(count === 0) {
g[count++] = 0.0
}
g.length = count
return g
}