Repository navigation
Add rounded division, remainder division, step rounding and a float factory - #25
Merged
Merged
Conversation
…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.
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)is0.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.17by5gives3and2; the remainder follows the sign rule ofmod().roundToStep($step, $roundMode): nearest multiple of any positive step.1.03to0.05is1.05.Decimal::createFromFloat($float): keeps every digit needed to reproduce the float. The constructor casts floats through theprecisionini setting and drops digits beyond the 14th by default (1234567890.1234567becomes1234567890.1235). The new factory is independent of ini settings and ofLC_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 of0keeps its decimals in 1.x, so its shares did not sum to the value. It now uses the decimals actually stored.