Published
Edited
Apr 26, 2022
2 stars
Insert cell
Insert cell
function kahansum(values) {
let summ = 0;
let c = 0;
for (const num of values) {
const y = num - c;
const t = summ + y;
c = t - summ - y;
summ = t;
}
return summ;
}
Insert cell
a = 10000.0
Insert cell
b = 3.14159
Insert cell
c = 2.71828
Insert cell
Insert cell
a + b + c
Insert cell
Insert cell
kahansum([a, b, c])
Insert cell
Insert cell
// fishing for a value that results in a floating point error. Turns out we're reinventing Number.EPSILON
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/EPSILON
// (thanks, @mootari!)
epsilon = {
let epsilon = 1.0;
while (1.0 + epsilon != 1.0) epsilon = epsilon / 2.0;
return epsilon; // Number.EPSILON/2
}
Insert cell
1.0 + epsilon - epsilon
Insert cell
kahansum([1.0, epsilon, -epsilon])
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more