Skip to content

fix: avoid NaN near negative poles in math/base/special/digamma and polygamma - #17133

Open
masrulmallick014-cpu wants to merge 1 commit into
stdlib-js:developfrom
masrulmallick014-cpu:fix-digamma-reflection
Open

masrulmallick014-cpu wants to merge 1 commit into
stdlib-js:developfrom
masrulmallick014-cpu:fix-digamma-reflection

Conversation

@masrulmallick014-cpu

@masrulmallick014-cpu masrulmallick014-cpu commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #17132

Description

What is the purpose of this pull request?

This pull request:

  • fixes math/base/special/digamma (JavaScript and C) so that, for x <= -1, the remainder for the reflection's tan term is computed from x itself before reflecting, instead of from the rounded 1 - x. This removes the NaN results and large errors near negative integers (e.g., digamma( -1.0000000000000002 ) and digamma( -4503599627370495.5 ) returned NaN). As in Boost, the term is evaluated as -pi * tan( pi * ( 0.5 - rem ) ) for 0.25 <= rem <= 0.75, where tan would otherwise lose digits.
  • fixes polycotpi in math/base/special/polygamma (JavaScript and C) to take cospi from the unrounded argument, as it already does for sinpi.
  • adds tests to both packages (JavaScript and native): digamma must not return NaN next to the negative integers -1 to -1000, and both functions are checked against mpmath values at the examples from the issue.
  • tightens the existing tolerances for negative x in polygamma from 2858 and 2773 ULP to 767 and 588 ULP.

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:

  • math/base/special/digammaf

What carried over from the reference(s)?

  • Computing the remainder before reflecting, which digammaf already does.

What differed, or what did you have to work out yourself?

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.

Checklist

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

  • Read, understood, and followed the [contributing guidelines][contributing].

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

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".

I applied the changes, ran the JavaScript and native tests locally, and checked the results myself.

…nd `polygamma`

Compute the remainder for the reflection's `tan` term from `x`
itself, before reflecting. `1 - x` is not always exact, so taking
the remainder after reflecting can lose the distance to the nearest
pole, which returns `NaN` or inaccurate values near negative
integers.

Apply the same change to `polycotpi` in `polygamma`, which took
`cospi` from the rounded `1 - x`.

This mirrors the upstream Boost fix (boostorg/math#1668).

---
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: passed
  - 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: passed
  - 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
---
@masrulmallick014-cpu
masrulmallick014-cpu requested a review from a team December 3, 2026 18:07
@stdlib-bot stdlib-bot added Math Issue or pull request specific to math functionality. Needs Review A pull request which needs code review. Good First PR A pull request resolving a Good First Issue. labels Dec 3, 2026
@stdlib-bot

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
math/base/special/digamma $\\color{red}527/534$
$\\color{green}+98.69\\%$
$\\color{red}36/39$
$\\color{green}+92.31\\%$
$\\color{green}6/6$
$\\color{green}+100.00\\%$
$\\color{red}527/534$
$\\color{green}+98.69\\%$
math/base/special/polygamma $\\color{red}1327/1372$
$\\color{green}+96.72\\%$
$\\color{red}135/158$
$\\color{green}+85.44\\%$
$\\color{green}17/17$
$\\color{green}+100.00\\%$
$\\color{red}1327/1372$
$\\color{green}+96.72\\%$

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

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

Good First PR A pull request resolving a Good First Issue. Math Issue or pull request specific to math functionality. Needs Review A pull request which needs code review.

Projects

None yet

2 participants