Skip to content

feat(tax_deduction_estimator): configure decimal rounding policies (#… - #590

Merged
2 commits merged into
Goldii-locks:mainfrom
EDOHWARES:feat/tax-estimator-rounding-policies
Sep 28, 2026
Merged

2 commits merged into
Goldii-locks:mainfrom
EDOHWARES:feat/tax-estimator-rounding-policies

Conversation

@EDOHWARES

Copy link
Copy Markdown

Description

Resolves #449

This PR configures consistent decimal rounding policies and remainder validation within tax_deduction_estimator. It introduces configurable rounding protocols (defaulting to Banker's rounding / round-to-nearest-even) for division operations and ensures intermediate division remainders are accurately preserved and validated.

Key Changes

  • Rounding Policies (src/utils/tax_deduction_estimator.ts):
    • Added RoundingPolicy supporting "half-even" / "round-to-nearest-even" (default), "half-up", "truncate", "ceil", and "floor".
    • Implemented roundIntegerDivision(numerator, divisor, mode) helper for integer-based rounding arithmetic.
    • Updated calculateTaxDeduction and estimateBracketTax to accept rounding policies via parameter or options object ({ roundingMode }).
    • Added ERROR_CODES.INVALID_ROUNDING_MODE (TAX_ESTIMATOR_INVALID_ROUNDING_MODE) for invalid policy rejection.
    • Integrated roundingMode into DbPrecisionSchema (for DB formatting) and TaxCsvExportOptions (for CSV exports).
  • Remainder Validation & Conservation Check:
    • Implemented verifyTaxDeductionRemainder (and alias validateTaxDeductionRemainder) to assert that intermediate division remainders are not dropped or lost (grossAmount * taxRate === unroundedTax * taxScale + remainder) and total funds are conserved (grossAmount === netAmount + taxAmount).
  • Test Suite (__tests__/tax_deduction_estimator.test.ts):
    • Added comprehensive test coverage for roundIntegerDivision across all rounding policies.
    • Verified default banker's rounding behavior, explicit policy overrides, invalid rounding mode error responses, DB storage formatting, and CSV export integration.

Verification & Testing

  • Type Check: Passed npx tsc --noEmit with 0 errors.
  • Build: Passed npm run build.
  • Unit Tests: Passed all 64 tests in __tests__/tax_deduction_estimator.test.ts and 100 tests in __tests__/tax_bracket_estimator.test.ts.

Closes #449
Closes #448
Closes #447
Closes #446

@drips-wave

drips-wave Bot commented Sep 27, 2026

Copy link
Copy Markdown

@EDOHWARES Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

godamongstmen897 pushed a commit that referenced this pull request Sep 28, 2026
godamongstmen897 pushed a commit that referenced this pull request Sep 28, 2026
#590 made half-even the default rounding policy for
calculateTaxDeduction and estimateBracketTax, which would silently
change the tax amount existing callers (routes, formatForDbStorage,
CSV export, bracket estimates) compute on halves and above-half
fractions. Keep "truncate" (plain bigint division, the pre-#590
behaviour) as the default for both functions and roundIntegerDivision;
the new policies stay available as an explicit opt-in.

Also fix roundIntegerDivision's "truncate" for negative numerators,
which rounded toward negative infinity like "floor"; it now rounds
toward zero, and "floor" keeps rounding down.

Update #590's tests: the default-policy test now asserts truncation,
the half-even cases pass the policy explicitly, and a new case covers
the default and truncate vs floor on negatives.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PJM5SuF3hAuKGMpPBz7Rwr
@godamongstmen897 godamongstmen897 closed this pull request by merging all changes into Goldii-locks:main in de734c9 Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants