Skip to content

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
josdejong:developfrom
JSap0914:fix/stirlings2-bignumber-return-type
Open

fix(combinatorics): stirlingS2 returns plain number instead of BigNumber for S(n,n) with n>0#3663
JSap0914 wants to merge 1 commit into
josdejong:developfrom
JSap0914:fix/stirlings2-bignumber-return-type

Conversation

@JSap0914

Copy link
Copy Markdown

Bug

stirlingS2(bignumber(n), bignumber(n)) returns a plain JS number (1) instead of a BigNumber when n > 0, even though both inputs are BigNumbers. The S(0,0) case is correct because it is initialised via cache[0] = [make(1)] (with make = bignumber in BigNumber mode). However, the diagonal entries for n > 0 are filled by:

if (i === m) {
  row[i] = 1          // BUG: plain literal, not make(1)
}

so the result is always the primitive 1 rather than bignumber(1).

Reproduction:

math.typeOf(math.stirlingS2(math.bignumber(3), math.bignumber(3)))
// 'number'  ← should be 'BigNumber'

Fix

Replace row[i] = 1 with row[i] = make(1) so the diagonal entry is created with the same factory function used everywhere else in the loop.

Verification

npx mocha test/unit-tests/function/combinatorics/stirlingS2.test.js
# 5 passing

Four regression tests added: stirlingS2(bn(1),bn(1)), stirlingS2(bn(3),bn(3)), stirlingS2(bn(7),bn(7)), and the mixed-input stirlingS2(bn(4),4) — all now return BigNumber(1).

AI-assisted contribution

…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).
Copilot AI review requested due to automatic review settings June 17, 2026 11:08

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.

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.

2 participants