Skip to content

Commit

Permalink
fix: rounding inconsistency for negative values in math.allocate
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy Brookes authored and MrShiny608 committed Jun 15, 2022
1 parent 984a2b1 commit 5b2e6b1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/utils/__tests__/math.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,7 @@ describe('allocate()', () => {
it('returns an array that distributes the provided number to each weight, using the negative weights to increase the amount that can be allocated', () => {
expect(allocate(100, [-5, 15], 10)).toEqual([-50, 150]);
});
it('returns the same distributions with negative and positive values', () => {
expect(allocate(21, [1, 5, 4], 10).map((value) => Math.abs(value))).toEqual(allocate(-21, [1, 5, 4], 10).map((value) => Math.abs(value)))
});
});
2 changes: 1 addition & 1 deletion src/utils/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function allocate(
let i = 0;
while (i < length - 1) {
const share = (weights[i] / total) * n + crumbs;
const wholeShare = Math.floor(share);
const wholeShare = Math.trunc(share);
crumbs = share - wholeShare;
sum += wholeShare;
shares[i] = wholeShare;
Expand Down

0 comments on commit 5b2e6b1

Please sign in to comment.