Skip to content

math.stirlingS2(n, i) and hence math.bellNumbers(n) rounding errors and incorrect values #2508

@brubsby

Description

@brubsby

I wrote the following line of js to reproduce this page from oeis:

console.log([...Array(27).keys()].reduce((prev, n)=>prev += "\n" + n.toString().padStart(2)+math.bellNumbers(n).toString().padStart(22), ""))

but the output has floating point errors and incorrect values

 0                     1
 1                     1
 2                     2
 3                     5
 4                    15
 5                    52
 6                   203
 7                   877
 8                  4140
 9                 21147
10                115975
11                678570
12               4213597
13              27644437
14             190899322
15            1382958545
16           10480142147
17     82864869804.00002
18     682076806158.9999
19         5832742205057
20        51724158235372
21    474869816156751.06
22      4506715738447324
23     44152005855084340
24    445958869294805250
25   4638590332230000000
26  49631246523618755000

attempting to work around this default behavior by passing in n as a bignumber also doesn't work:

console.log([...Array(27).keys()].reduce((prev, n)=>prev += "\n" + n.toString().padStart(2)+math.bellNumbers(math.bignumber(n)).toString().padStart(22), ""))
Uncaught TypeError: Cannot implicitly convert a number with >15 significant digits to BigNumber (value: 51090942171709440000). Use function bignumber(x) to convert to BigNumber.
    at convert (math.js:40:116329)
    at Array.C.t (math.js:40:28692)
    at Function.r (math.js:40:30983)
    at Function.ae (math.js:40:31507)
    at divideScalar (math.js:40:31921)
    at Function.number | BigNumber, number | BigNumber (math.js:40:555793)
    at stirlingS2 (math.js:40:31734)
    at Function.number | BigNumber (math.js:40:556157)
    at Object.bellNumbers (math.js:40:31680)
    at <anonymous>:1:98

both of which seem like incorrect behaviors

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions