Skip to content

Commit

Permalink
✨ installments: strictly match total amount
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzdanilo committed Oct 18, 2024
1 parent 67f1016 commit e6ef876
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/installments/split.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export default function splitInstallments(
const diffs = sub(installments, mean(installments));
amounts = sub(amounts, mulDivUp(diffs, weight, WAD));
amounts = mulDivUp(amounts, totalAmount, sum(amounts));
const excess = sum(amounts) - totalAmount;
for (let index = 0; index < excess; ++index) amounts[index]! -= 1n; // eslint-disable-line @typescript-eslint/no-non-null-assertion
error = mean(mulDivUp(abs(diffs), weight, WAD));
} while (error >= tolerance);

Expand Down
3 changes: 1 addition & 2 deletions test/installments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ describe("installments", () => {
);

expect(amounts).toHaveLength(count);
expect(sum(amounts)).toBeGreaterThanOrEqual(totalAmount);
expect(sum(amounts) - totalAmount).toBeLessThanOrEqual(count);
expect(sum(amounts)).toStrictEqual(totalAmount);
expect(effectiveRate).toBeGreaterThanOrEqual(min(rates));
expect(effectiveRate).toBeLessThanOrEqual(max(rates));

Expand Down

0 comments on commit e6ef876

Please sign in to comment.