Published unlisted
Edited
Jun 24, 2020
Importers
Insert cell
Insert cell
t = evaluate(sum, 2500)
Insert cell
k = evaluate(kahansum, 1500)
Insert cell
f = evaluate(fsum, 500)
Insert cell
evaluate(fsum_1, 3500)
Insert cell
t.s - k.s
Insert cell
t.t / k.t
Insert cell
t.t / f.t
Insert cell
numbers = Float64Array.from({ length: 1e7 }, (_,i ) => Math.random()-.5)
Insert cell
evaluate = async (fun, delay) => {
await Promises.delay(delay);
const t = performance.now();
const s = +fun(numbers);
const v = +fun([14, 1, 2, 3], (d, i) => i === 0 ? -d : d * d); // valueof accessor
return { s, t: performance.now() - t,
v: v ? "🧨" : "👍",
r0: +fun([.3, .3, .3, .3, .3, .3, .3, .3, .3, .3, -.3, -.3, -.3, -.3, -.3, -.3, -.3, -.3, -.3, -.3])
? "🧨" : "👍",
r1: +fun([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1, -1.]) // "🧨" or "👍"? fsum is probably correct
};
}
Insert cell
function kahansum(values, valueof) {
let sum0 = 0,
sum1,
error = 0;
if (valueof === undefined) {
for (let value of values) {
if ((value = +value)) {
sum1 = sum0 + (value -= error);
error = sum1 - sum0 - value;
sum0 = sum1;
}
}
} else {
let index = -1;
for (let value of values) {
if ((value = +valueof(value, ++index, values))) {
sum1 = sum0 + (value -= error);
error = sum1 - sum0 - value;
sum0 = sum1;
}
}
}
return sum0;
}
Insert cell
{
const A = [1, 2, 3];
A.length = 1;
A.push(5);
return A;
}
Insert cell
+fsum_1([.1, .1])
.add([.1, .1, .1, .1])
.add(.1)
.add([-.1, -.1, -.1])
.add(-.1)
.add([-.1, -.1, -.1])
Insert cell
import {fsum} from "727130e216dc8cd4"
Insert cell
Insert cell
// adapted from https://github.com/python/cpython/blob/master/Modules/mathmodule.c#L1444
function fsum_1(iterable, valueof) {
const partials = [];

const isIterable = object =>
object != null && typeof object[Symbol.iterator] === 'function';

const Adder = {
partials,
add: function(t) {
const p = partials;
let n = p.length, i, j, tmp, y, hi, lo, index = -1;
const values = isIterable(t) ? t : [t];
if (valueof) for (let x of values) {
x = valueof(x, ++index, values)
for (i = j = 0; j < n && j < 32; j++) {
if (Math.abs(x) < Math.abs((y = p[j]))) (tmp = x), (x = y), (y = tmp);
hi = x + y;
lo = y - (hi - x);
if (lo) p[i++] = lo;
x = hi;
}
n = i;
p[n++] = x;
}
else for (let x of values) {
for (i = j = 0; j < n && j < 32; j++) {
if (Math.abs(x) < Math.abs((y = p[j]))) (tmp = x), (x = y), (y = tmp);
hi = x + y;
lo = y - (hi - x);
if (lo) p[i++] = lo;
x = hi;
}
n = i;
p[n++] = x;
}
p.splice(n, 32);
return Adder;
},
valueOf: function() {
let n = partials.length,
x,
y,
hi = 0,
lo;
if (n > 0) {
hi = partials[--n];
while (n > 0) {
x = hi;
y = partials[--n];
hi = x + y;
lo = y - (hi - x);
if (lo) break;
}
if (
n > 0 &&
((lo < 0 && partials[n - 1] < 0) || (lo > 0 && partials[n - 1] > 0))
) {
y = lo * 2.0;
x = hi + y;
if (y == x - hi) hi = x;
}
}
return hi;
},
reset: function() {
partials.length = 0;
}
};

if (iterable) Adder.add(iterable);
return Adder;
}
Insert cell
// adapted from https://github.com/python/cpython/blob/master/Modules/mathmodule.c#L1444
function fsum_direct(iterable) {
const p = new Float64Array(32);
let i,
j,
x,
y,
yr,
tmp,
hi,
lo,
n = 0;

for (x of iterable) {
for (i = j = 0; j < n && j < 32; j++) {
if (Math.abs(x) < Math.abs((y = p[j]))) (tmp = x), (x = y), (y = tmp);

hi = x + y;
yr = hi - x;
lo = y - yr;
if (lo) p[i++] = lo;
x = hi;
}

n = i;
p[n++] = x;
}

hi = 0;
if (n > 0) {
hi = p[--n];
while (n > 0) {
x = hi;
y = p[--n];
hi = x + y;
yr = hi - x;
lo = y - yr;
if (lo) break;
}
if (n > 0 && ((lo < 0 && p[n - 1] < 0) || (lo > 0 && p[n - 1] > 0))) {
y = lo * 2.0;
x = hi + y;
yr = x - hi;
if (y == yr) hi = x;
}
}

return hi;
}
Insert cell
function sum(values, valueof) {
let sum = 0;
if (valueof === undefined) {
for (let value of values) {
if ((value = +value)) {
sum += value;
}
}
} else {
let index = -1;
for (let value of values) {
if ((value = +valueof(value, ++index, values))) {
sum += value;
}
}
}
return sum;
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more