0.1.0 • Published 4 years ago
round-preserving-sum v0.1.0
Round Preserving Sum
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
Installation
npm install --save round-preserving-sumUsage
const roundPreservingSum = require('round-preserving-sum')
console.log(roundPreservingSum([0.5, 0.5, 0.5, 0.5]))
// => [ 1, 1, 0, 0 ]API
roundPreservingSum(input)
input(number[] | Float32Array | Float64Array, required)- returns
number[] | Float32Array | Float64Array- a new array with the numbers rounded
Fuzzing
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
0.1.0
4 years ago