fix(combinatorics): stirlingS2 returns plain number instead of BigNumber for S(n,n) with n>0#3663
Open
JSap0914 wants to merge 1 commit into
Open
Conversation
…with BigNumber input
When n === k > 0, the recurrence table filler assigned the plain number
literal 1 instead of make(1) (which produces a BigNumber in BigNumber
mode). As a result stirlingS2(bignumber(n), bignumber(n)) incorrectly
returned the plain JS number 1 instead of a BigNumber.
The S(0,0) case was already correct because it is handled via
cache[0] = [make(1)]
outside the inner loop.
Fix: replace row[i] = 1 with row[i] = make(1) in the diagonal branch
of the recurrence loop.
Add four regression tests that assert BigNumber return type for
S(n,n) with n in {1,3,7} (both-BigNumber) and n=4 (mixed input).
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.
Bug
stirlingS2(bignumber(n), bignumber(n))returns a plain JSnumber(1) instead of aBigNumberwhenn > 0, even though both inputs are BigNumbers. TheS(0,0)case is correct because it is initialised viacache[0] = [make(1)](withmake = bignumberin BigNumber mode). However, the diagonal entries forn > 0are filled by:so the result is always the primitive
1rather thanbignumber(1).Reproduction:
Fix
Replace
row[i] = 1withrow[i] = make(1)so the diagonal entry is created with the same factory function used everywhere else in the loop.Verification
Four regression tests added:
stirlingS2(bn(1),bn(1)),stirlingS2(bn(3),bn(3)),stirlingS2(bn(7),bn(7)), and the mixed-inputstirlingS2(bn(4),4)— all now returnBigNumber(1).