round-preserving-sum
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

Round Preserving Sum

A small package to round an array of numbers, whilst still preserving the sum of all numbers.

Relevant reading:

Installation

npm install --save round-preserving-sum

Usage

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

Readme

Keywords

none

Package Sidebar

Install

npm i round-preserving-sum

Weekly Downloads

12

Version

0.1.0

License

MIT

Unpacked Size

4.33 kB

Total Files

6

Last publish

Collaborators

  • linusu