A small package to round an array of numbers, whilst still preserving the sum of all numbers.
Relevant reading:
- How to round floats to integers while preserving their sum?
- Round vector of numerics to integer while preserving their sum
- Rounding numbers preserving their sum
npm install --save round-preserving-sum
const roundPreservingSum = require('round-preserving-sum')
console.log(roundPreservingSum([0.5, 0.5, 0.5, 0.5]))
// => [ 1, 1, 0, 0 ]
-
input
(number[] | Float32Array | Float64Array
, required) - returns
number[] | Float32Array | Float64Array
- a new array with the numbers rounded
The library has been fuzzed to make sure that the following guarantees holds:
- The rounded sum of the input numbers is always equal to the sum of the output numbers
- Every value in the output is either the floored or ceiled value of the corresponding input
- The accumulated rounding error is never more than half of the input length