Skip to content

fix: avoid scale-sized allocations when reducing decimals - #120

Open
simPod wants to merge 2 commits into
brick:mainfrom
simPod:fix/decimal-downscaling
Open

simPod wants to merge 2 commits into
brick:mainfrom
simPod:fix/decimal-downscaling

Conversation

@simPod

@simPod simPod commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Reducing a decimal with a small coefficient and a very large scale pads the coefficient to the full source scale. At PHP_INT_MAX, calculating the padding length overflows and str_pad() throws a TypeError. The same path is used by toScale(), toBigInteger(), and degree-one nthRoot().

Check the coefficient’s trailing digits directly for exact reductions. When more digits are discarded than the coefficient contains, the nonzero magnitude is strictly below half a target unit. Round an equivalent signed tenth rather than constructing a divisor proportional to the scale. Keep the ordinary rounding path when a midpoint is possible.

@simPod
simPod marked this pull request as ready for review November 26, 2026 08:08
@simPod
simPod force-pushed the fix/decimal-downscaling branch from 351f1e9 to 8360254 Compare December 1, 2026 09:52
@codecov

codecov Bot commented Dec 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (07741d2) to head (8360254).

Additional details and impacted files
@@              Coverage Diff              @@
##               main      #120      +/-   ##
=============================================
+ Coverage     99.94%   100.00%   +0.05%     
  Complexity      824       824              
=============================================
  Files            21        21              
  Lines          1971      1966       -5     
=============================================
- Hits           1970      1966       -4     
+ Misses            1         0       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant