Skip to content

feature: Add function to calculate Bernoulli numbers#3551

Merged
josdejong merged 9 commits into
josdejong:developfrom
gwhitney:bernoulli_numbers
Oct 29, 2025
Merged

feature: Add function to calculate Bernoulli numbers#3551
josdejong merged 9 commits into
josdejong:developfrom
gwhitney:bernoulli_numbers

Conversation

@gwhitney
Copy link
Copy Markdown
Collaborator

I am working on #3532. I thought it was a quick fix, but diving into the details reveals that there were other accuracy issues, e.g. with zeta(bignumber(-7)). Fixing these means more use of the reflection formula, which in turn means that the inability to compute the gamma and/or lgamma functions for non-integer BigNumbers will be more restrictive (since the reflection formula uses gamma).

So my plan now is to implement the gamma function to BigNumber precision (hopefully; there are some articles on this but I don't have all the details worked out). The leading method seems to be the Stirling formula, which in turn makes use of the Bernoulli numbers. Hence this PR provides an implementation of a function bernoulli(n) for computing the nth Bernoulli number, whether n is a number, BigNumber, or Fraction (returning the same type as its argument).

This is a self-contained, independently valuable addition as the Bernoulli numbers appear in multiple different mathematical contexts.

@gwhitney
Copy link
Copy Markdown
Collaborator Author

Oops, I just realized that since the index is an integer, bigint input should be allowed as well, in which case it just returns the exact fraction (same as Fraction input), and that I should test the illegal arguments as well, All should be well now for review. Thanks.

Copy link
Copy Markdown
Owner

@josdejong josdejong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome @gwhitney , this is a nice addition! It is well tested and documented, looks good!

I made a few inline comments, can you have a look at those?

Comment thread src/function/probability/bernoulli.js Outdated
Comment thread types/index.d.ts
@gwhitney
Copy link
Copy Markdown
Collaborator Author

OK, I think that should do it.

@josdejong
Copy link
Copy Markdown
Owner

Thanks, that did work out nicely!

Two small things:

  1. I think you accidentally committed a file bernoulli.js~, can you remove that again?
  2. Not a big thing but a personal preference: can you move the _bernoulli function to the bottom of the file? That way, when you open the file, you first see the high level typed function, and after that the low level _bernoulli function. I think that is a more natural order to reading the file.

@gwhitney
Copy link
Copy Markdown
Collaborator Author

Of course, will do. Is it OK if I add a '*~' to the .gitignore for Emacs backup files? I've been dealing with combing them out of my commits for a couple of years now, and this is the first time I have tripped up, sorry, but it would save me a lot of pain if mathjs could just gitignore such files...

@gwhitney
Copy link
Copy Markdown
Collaborator Author

(OK I have done all those things including adding to .gitignore, feel free to back out the change to .gitignore if you prefer not to have it.)

@gwhitney
Copy link
Copy Markdown
Collaborator Author

2. I think that is a more natural order to reading the file.

Shows how natural is in the eye of the beholder. When I open the code, I like to see the actual algorithm over the window dressing ;-) but of course am fine with either order.

@josdejong
Copy link
Copy Markdown
Owner

Eh, yeah, that's true, the order of functions in a file is quite subjective and a matter of personal preference. Thanks for updating the code!

@josdejong josdejong merged commit faf249b into josdejong:develop Oct 29, 2025
8 checks passed
@josdejong
Copy link
Copy Markdown
Owner

Published now in v15.1.0

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