Skip to content

Add rounded division, remainder division, step rounding and a float factory - #25

Merged
dereuromark merged 1 commit into
masterfrom
feature/divide-rounding
Oct 11, 2026
Merged

dereuromark merged 1 commit into
masterfrom
feature/divide-rounding

Conversation

@dereuromark

Copy link
Copy Markdown
Contributor

Five additions picked from a comparison with brick/math, BcMath\Number, php-decimal and moneyphp. Tests and documentation pages are included.

Nothing existing changes: master and this branch return the same result for all 10,339 calls of the comparison run.

  • divide($value, $scale, $roundMode): optional third argument. Without it the quotient is cut off as before; with it, Decimal::create('2')->divide('3', 2, Decimal::ROUND_HALF_UP) is 0.67. The decision is made on whole numbers from the exact remainder, so ties are detected exactly at any scale, and all eight modes work.
  • divideWithRemainder($value): whole number quotient and remainder. 17 by 5 gives 3 and 2; the remainder follows the sign rule of mod().
  • roundToStep($step, $roundMode): nearest multiple of any positive step. 1.03 to 0.05 is 1.05.
  • Decimal::createFromFloat($float): keeps every digit needed to reproduce the float. The constructor casts floats through the precision ini setting and drops digits beyond the 14th by default (1234567890.1234567 becomes 1234567890.1235). The new factory is independent of ini settings and of LC_NUMERIC.
  • Decimal::zero(), Decimal::one()

One fix to allocate() from the previous PR, unreleased so far: it used the declared scale as its unit. A value created with a scale of 0 keeps its decimals in 1.x, so its shares did not sum to the value. It now uses the decimals actually stored.

…actory

All additions; the 10,000 call comparison against master shows no
difference for existing calls.

- divide() takes an optional rounding mode as third argument. Without
  it the quotient is cut off as before. With it the rounding is decided
  on whole numbers from the exact remainder, so ties are detected
  exactly at any scale.
- divideWithRemainder() returns the whole number quotient and the
  remainder, with the same sign rule as mod().
- roundToStep() rounds to a multiple of any positive step, such as 0.05.
- createFromFloat() keeps all digits needed to reproduce the float. The
  constructor casts floats through the precision ini setting and drops
  digits beyond the 14th by default. The conversion does not depend on
  LC_NUMERIC.
- zero() and one().

allocate() used the declared scale as its unit. A value created with a
scale of 0 keeps its decimals in 1.x, so its shares did not sum up. It
now uses the decimals actually stored.
Copilot AI balanced review requested due to automatic review settings October 11, 2026 15:06
@dereuromark dereuromark added the enhancement New feature or request label Oct 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@codecov

codecov Bot commented Oct 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.53%. Comparing base (a1ec406) to head (392aed5).

Additional details and impacted files
@@             Coverage Diff              @@
##             master      #25      +/-   ##
============================================
+ Coverage     97.20%   97.53%   +0.33%     
- Complexity      176      195      +19     
============================================
  Files             1        1              
  Lines           358      406      +48     
============================================
+ Hits            348      396      +48     
  Misses           10       10              

☔ 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.

@dereuromark
dereuromark merged commit 348c9b2 into master Oct 11, 2026
7 checks passed
@dereuromark
dereuromark deleted the feature/divide-rounding branch October 11, 2026 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants