Public
Edited
Mar 13, 2023
Insert cell
Insert cell
aggregateData = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 292, 224, 709, 5510, 47748, 113271, 393096, 434984, 647779, 555893, 886585,
1169044, 1359162, 990454, 1336195, 1321434, 1297254, 951361, 1214077, 1081038,
990117, 722211, 983244, 972272, 927088, 653675, 812555, 745563, 704142,
511394, 671522, 650070, 638144, 473030, 624715, 598408, 561451, 395833,
496390, 453236, 406751, 270281, 319426, 278963, 249639, 172300, 218893,
208241, 199446, 143300, 185037, 180551, 178023, 132377, 173569, 165617,
156969, 110887, 140765, 133122, 125656, 89526, 112978, 106344, 99865, 70781,
90205, 84223, 80869, 59040, 77026, 75916, 74602, 56802, 76724, 78002, 78559,
58404, 77282, 73771, 69881, 49410, 63095, 58190, 53719, 37843, 47115, 43397,
39929, 27416, 33086, 28707, 25003, 16490, 18922, 16307, 13473, 8677, 9679,
8108, 6636, 3960, 4529, 3652, 2866, 1915, 2211, 1903, 1756, 1265, 1675, 1628,
1641, 1191, 1485, 1333, 1251, 883, 1075, 1093, 1126, 972, 1302, 1464, 1675,
1164, 1431, 1377, 1238, 865, 1092, 1082, 1024, 771, 905, 813, 725, 493, 649,
551, 534, 407, 563, 568, 545, 437, 523, 482, 431, 258, 268, 243, 215, 134,
192, 172, 152, 135, 166, 158, 198, 147, 188, 171, 138, 107, 123, 106, 80, 38,
53, 39, 33, 18, 15, 18, 7, 5, 6, 3, 6, 2, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0,
1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]
Insert cell
Insert cell
Insert cell
lastElement = (arr) => arr[arr.length - 1]
Insert cell
{
const float32AggregateData = new Float32Array(aggregateData);
const float64AggregateData = new Float64Array(aggregateData);
const uint32AggregateData = new Uint32Array(aggregateData);

cumulativeSum(float32AggregateData);
cumulativeSum(float64AggregateData);
cumulativeSum(uint32AggregateData);

const results = {
correct: regularSum(aggregateData),
float32LastElement: lastElement(float32AggregateData), // problem!
float64LastElement: lastElement(float64AggregateData),
uint32LastElement: lastElement(uint32AggregateData)
};

console.table(results);

return results;
}
Insert cell
Insert cell
{
// show it can exceed the problematic number
const problemNumber = 33803104;
const test = new Float32Array([problemNumber, problemNumber]);
cumulativeSum(test);
return test;
}
Insert cell
Insert cell
{
const problemNumber = 33803104;
const data = new Float32Array(1_000_000).fill(problemNumber);
cumulativeSum(data);
return lastElement(data);
}
Insert cell
Insert cell
Insert cell
{
const aggregateFloat32Array = new Float32Array(aggregateData);

// write out cumsum very verbosely
for (let i = 0, j = 1; j < aggregateFloat32Array.length; i++, j++) {
const prevCumSum = aggregateFloat32Array[i];
const curNumToAdd = aggregateFloat32Array[j];
const newCumSum = prevCumSum + curNumToAdd;

// IM CLEARLY OVERRIDING THIS PLACE IN ARRAY WITH NEW SUM
aggregateFloat32Array[j] = newCumSum;

// check when we have an error
const insertedIntoArrayCorrectly = newCumSum === aggregateFloat32Array[j];
if (!insertedIntoArrayCorrectly) {
console.table({
prevCumSum,
curNumToAdd,
newCumSum,
newArrayValue: aggregateFloat32Array[j],
index: j
});
throw new Error(
`Fails at index ${j}, where ${aggregateFloat32Array[j]} is supposed to be ${newCumSum}`
);
}
}

return lastElement(aggregateFloat32Array);
}
Insert cell
Insert cell
Insert cell
{
// here I don't trust Float32Array constructor
const aggregateDataFloat32Array = new Float32Array(aggregateData.length);
for (let i = 0; i < aggregateData.length; i++) {
aggregateDataFloat32Array[i] = aggregateData[i];
}
cumulativeSum(aggregateDataFloat32Array);

return lastElement(aggregateDataFloat32Array);
}
Insert cell
Insert cell
Insert cell
{
const prevCumSum = 18_401_042;
const curNumToAdd = 653675;
const test = new Float32Array([prevCumSum, curNumToAdd]);

const newCumSum = test[0] + test[1];
test[1] = newCumSum;
return {
prevCumSum,
curNumToAdd,
newCumSum,
update: test[1]
};
}
Insert cell
What about other numbers?
Insert cell
{
const prevCumSum = 18_401_042;
const curNumToAdd = 1;
const test = new Float32Array([prevCumSum, curNumToAdd]);

const newCumSum = test[0] + test[1];
test[1] = newCumSum;

return {
prevCumSum,
curNumToAdd,
newCumSum,
update: test[1]
};
}
Insert cell
{
const prevCumSum = 18_401_042;
const curNumToAdd = 2;
const test = new Float32Array([prevCumSum, curNumToAdd]);

const newCumSum = test[0] + test[1];
test[1] = newCumSum;

return {
prevCumSum,
curNumToAdd,
newCumSum,
update: test[1]
};
}
Insert cell
Insert cell
Insert cell
{
const prevCumSum = 10_401_042;
const curNumToAdd = 20;
const test = new Float32Array([prevCumSum, curNumToAdd]);

test[1] += test[0];

return {
prevCumSum,
curNumToAdd,
update: test[1]
};
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const test = new BigUint64Array(aggregateData.map((d) => BigInt(d)));
cumulativeSum(test);
return test.at(-1);
}
Insert cell
Insert cell
{
const aggregateFloat64Array = new Float64Array(aggregateData);
cumulativeSum(aggregateFloat64Array);
const storageFloat32Array = new Float32Array(aggregateFloat64Array);
return {
float64: lastElement(aggregateFloat64Array),
storefloat32: lastElement(storageFloat32Array)
};
}
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