fix(round): round negative Fractions half away from zero - #3702
Open
yu2971512385-ui wants to merge 1 commit into
Open
yu2971512385-ui wants to merge 1 commit into
yu2971512385-ui wants to merge 1 commit into
Conversation
round() resolved a tie differently depending on the value's type:
number and BigNumber round a half away from zero, while Fraction (and
so also 'round(fraction(x), n)') rounds it towards positive infinity,
because Fraction.round follows Math.round:
math.round(-2.5) // -3
math.round(math.bignumber(-2.5)) // -3
math.round(math.fraction(-2.5)) // -2 <-
math.round(-0.25, 1) // -0.3
math.round(math.fraction(-0.25), 1) // -0.2 <-
Round the magnitude and re-apply the sign so all three types agree.
Positive fractions are unaffected.
This branch has not been deployed
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.
roundresolves a tie differently depending on the type of the value:number and BigNumber round a half away from zero (
roundNumber, and decimal.js's defaultROUND_HALF_UPwhich rounds away from zero).Fraction.roundfollowsMath.roundinstead, which rounds a half towards positive infinity, so every exact half with a negative fraction lands on the other side. Positive values agree across all three types, which is why this is easy to miss.floor,ceilandfixare already consistent across the three types;roundis the one that is not.Change
roundFractionrounds the magnitude and re-applies the sign, and the threeFractionsignatures use it. Positive fractions are untouched, and the existing behaviour fornumber,bigint,BigNumber,Unitand matrices is unchanged.Tests
A new case in
test/unit-tests/function/arithmetic/round.test.jscovers-1/2,-3/2,-5/2,-2/3,-1/3, and-1/4with 1 decimal (asnumberand asBigNumber), and asserts the same expectations against the number and BigNumber implementations so the three stay pinned together. The new test fails before the change;npx mocha test/unit-tests --recursivepasses after it (6653 passing), andeslintis clean.Note on Complex
Complex.roundhas the same tie behaviour (math.round(math.complex(-2.5, -2.5))→-2 - 2i), since complex.js also usesMath.round. Fixing that means rounding the parts in mathjs rather than delegating, which changes more than this fix — happy to follow up with a separate PR if you would like that made consistent too.