Skip to content

fix: correct off-by-one in large-x series of math/base/special/gammainc - #16534

Open
manit2004 wants to merge 1 commit into
stdlib-js:developfrom
manit2004:fix/gammainc-large-x-series
Open

manit2004 wants to merge 1 commit into
stdlib-js:developfrom
manit2004:fix/gammainc-large-x-series

Conversation

@manit2004

@manit2004 manit2004 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #16533.

Description

What is the purpose of this pull request?

This pull request:

  • Fixes an off-by-one in the large-x asymptotic series used by math/base/special/gammainc. The expansion is 1 + (a-1)/x + (a-1)(a-2)/x² + ..., so the falling factorial has to start at a-1 (Boost does a_poch( a - 1 )), but both the JS and C implementations started at a.
  • Adds regression tests for gammainc and for poisson/cdf and chisquare/cdf, which are affected through it. The existing fixtures pass with or without the fix, which is why this went unnoticed.

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

Reference Packages

Did the content of this pull request follow one or more existing packages? Answering is optional.

This pull request followed these packages:

  • None, this is a bug fix.

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

Call Before After Expected (mpmath, 60 digits)
gammainc( 1367.3436948118986, 1409.203708740708 ) 0.09952119051642438 0.13190499189857297 0.13190499189857088
gammainc( 1001, 1000.5, true, true ) 0.501850998882592 0.48949191460024466 0.48949191460024505
poisson cdf( 999, 1001 ) 0.49530155533410974 0.48318854707021264 0.4831885470702138
chisquare cdf( 2500, 2400 ) 0.9231500432608719 0.9241270884571918 0.9241270884571916

The new tests fail on the unfixed code and pass with the fix, for both the JS and native implementations (I rebuilt the three add-ons with and without the C change).

I also compared against SciPy on 20,000 points (x from 1e3 to 1e6, a near x). Points with a relative error above 1e-8 went from 13,130 to 40. The remaining 40 are around x ≈ 975000, with a worst relative error of 4.68e-6. I haven't looked into those, and they may be a separate issue.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

@stdlib-js/reviewers

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: skipped
  - task: lint_markdown_pkg_readmes
    status: na
  - task: lint_markdown_docs
    status: na
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: missing_dependencies
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
@manit2004
manit2004 requested a review from a team November 7, 2026 15:12
@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label Nov 7, 2026
@stdlib-bot

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
math/base/special/gammainc $\\color{red}1794/1978$
$\\color{green}+90.70\\%$
$\\color{red}133/161$
$\\color{green}+82.61\\%$
$\\color{red}24/28$
$\\color{green}+85.71\\%$
$\\color{red}1794/1978$
$\\color{green}+90.70\\%$
stats/base/dists/chisquare/cdf $\\color{green}241/241$
$\\color{green}+100.00\\%$
$\\color{green}7/7$
$\\color{green}+100.00\\%$
$\\color{green}3/3$
$\\color{green}+100.00\\%$
$\\color{green}241/241$
$\\color{green}+100.00\\%$
stats/base/dists/poisson/cdf $\\color{green}286/286$
$\\color{green}+100.00\\%$
$\\color{green}28/28$
$\\color{green}+100.00\\%$
$\\color{green}4/4$
$\\color{green}+100.00\\%$
$\\color{green}286/286$
$\\color{green}+100.00\\%$

The above coverage report was generated for the changes in this PR.

@kgryte
kgryte requested a review from anandkaranubc November 8, 2026 22:58
@kgryte

kgryte commented Nov 8, 2026

Copy link
Copy Markdown
Member

@anandkaranubc Would you mind taking a first look at this? Cheers!

@kgryte kgryte added Math Issue or pull request specific to math functionality. Numerical Accuracy Issue or pull request concerns numerical accuracy. labels Nov 8, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Math Issue or pull request specific to math functionality. Needs Review A pull request which needs code review. Numerical Accuracy Issue or pull request concerns numerical accuracy.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: math/base/special/gammainc is off by up to 0.03 for x > 1000 when a is close to x

3 participants