-
Notifications
You must be signed in to change notification settings - Fork 436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: use method of largest remainder to count poll votes #13554
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quota methods is a good solution, yet I'd recommend to simplify its implementation for a better understanding
if (!total) {
return votes
}
const { wholes, remainders } = votes.reduce((acc: VoteAccumulator, vote) => {
const quota = vote / total * 100
acc.wholes.push(Math.floor(quota))
acc.remainders.push(quota % 1)
return acc
}, { wholes: [], remainders: [] })
// Calculate the sum of whole parts
const sumWholes = wholes.reduce((acc, value) => acc + value, 0)
// If the sum of whole parts is already 100, return it
if (sumWholes === 100) {
return wholes
}
// Calculate how many more votes we need to reach 100
let remainingVotes = 100 - sumWholes
// Get the indexes of the largest remainders
const largestRemainders = remainders
.map((value, index) => ({ value, index }))
.sort((a, b) => b.value - a.value)
.slice(0, remainingVotes)
.map(item => item.index)
// Distribute the remaining votes
for (const index of largestRemainders) {
wholes[index]++
}
return wholes
cbcb05a
to
b522bb6
Compare
This comment was marked as off-topic.
This comment was marked as off-topic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested, we agreed to prioritize fair accuracy than mainly targeting 100%.
Signed-off-by: Maksim Sukharev <[email protected]>
b522bb6
to
a17b043
Compare
/backport to stable30 |
☑️ Resolves
🖌️ UI Checklist
🖼️ Screenshots / Screencasts
🏁 Checklist