Merge pull request #4531 from rizka10/master
Improve summing of poll percentages to 100
This commit is contained in:
commit
71fa9541fa
|
@ -1,17 +1,29 @@
|
||||||
// stolen from http://stackoverflow.com/a/13484088/11983
|
// works as described on http://stackoverflow.com/a/13483710
|
||||||
function sumsUpTo100(percentages) {
|
function sumsUpTo100(percentages) {
|
||||||
return percentages.map(p => Math.floor(p)).reduce((a, b) => a + b) === 100;
|
return percentages.map(p => Math.floor(p)).reduce((a, b) => a + b) === 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default (percentages) => {
|
export default (percentages) => {
|
||||||
const sumOfDecimals = Math.ceil(percentages.map(a => a % 1).reduce((a, b) => a + b));
|
var decimals = percentages.map(a => a % 1);
|
||||||
// compensate error by adding 1 to the first n "non-zero" items
|
const sumOfDecimals = Math.ceil(decimals.reduce((a, b) => a + b));
|
||||||
for (let i = 0, max = percentages.length; i < sumOfDecimals && i < max; i++) {
|
// compensate error by adding 1 to n items with the greatest decimal part
|
||||||
if (percentages[i] > 0) {
|
for (let i = 0, max = decimals.length; i < sumOfDecimals && i < max; i++) {
|
||||||
percentages[i] = ++percentages[i];
|
// find the greatest item in the decimals array, set it to 0,
|
||||||
// quit early when there is a rounding issue
|
// and increase the corresponding item in the percentages array by 1
|
||||||
if (sumsUpTo100(percentages)) break;
|
let greatest = 0;
|
||||||
|
let index = 0;
|
||||||
|
for (let j=0; j < decimals.length; j++) {
|
||||||
|
if (decimals[j] > greatest) {
|
||||||
|
index = j;
|
||||||
|
greatest = decimals[j];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
++percentages[index];
|
||||||
|
decimals[index] = 0;
|
||||||
|
// quit early when there is a rounding issue
|
||||||
|
if (sumsUpTo100(percentages)) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return percentages.map(p => Math.floor(p));
|
return percentages.map(p => Math.floor(p));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue