Skip to content

Conversation

tedgin
Copy link
Contributor

@tedgin tedgin commented Aug 17, 2025

The implementation std.mathspecial.beta(x, y) of B(x,y) is extended to handle its edge cases that occur when using the main algorithm gamma(x) * gamma(y) / gamma(x + y) to compute B(x,y). Theses cases happen in the regions of the (x,y) plane where gamma(x), gamma(y), or gamma(x+y) is not finite. These cause beta(x, y) to return NaN in regions where B(x,y) exists. This PR extends beta to cover the following cases.

  • usage of the large value algorithm involving logGamma is extended to every region where |Γ(x)|, |Γ(y)|, or |Γ(x+y)| is too large to be represented with a real
  • x+y is a non-positive integer, where Γ(x+y) does not exist, but 1/Γ(x+y) = 0
  • either x or y is +0 with the other value being nonnegative and finite
  • x,y > 0 and x or y is infinite
  • either x or y is -0 with the other value being positive
  • either x or y is negative, finite, and not an integer and the other value is +∞
  • either x or y is +0 and the other value is negative, finite, and not an integer
  • either x or y is -0 and the other value is negative, finite, and not an integer

Extending beta in this way required the recent bug fixes to beta and sgnGamma to be in place, so these where cherry picked from the stable branch into this PR.

The resulting implementation of beta is no longer a few lines of code, so it was moved into a new eponymous function in the module std.internal.math.gammafunction. This required the implementation of sgnGamma to be moved into an new eponymous function in the same module as well. I made this a separate commit, in case I shouldn't have done this.

The larger value algorithm for compute B is used two times in the new implementation of beta, so I moved this logic out of beta and into a new function betaLarge. I made this a separate commit, in case I shouldn't have done this.

tedgin added 2 commits August 16, 2025 18:04
Use of rndtol to determine which domain x belongs to is replaced with
trunc. rndtol depends on rounding mode. Depending on the mode,
rndtol(-1.5) could be -2 or -1, which map to different domains with
different signs. trunc doesn't depend on rounding mode. trunc(-1.5) is
always -1.
Fix the large value method of computing std.mathspecial.beta so that it
recovers the signs of gamma(x), gamma(y), and gamma(x+y).
@tedgin tedgin requested a review from ibuclaw as a code owner August 17, 2025 02:27
@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @tedgin! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

⚠️⚠️⚠️ Warnings ⚠️⚠️⚠️

  • In preparation for migrating from Bugzilla to GitHub Issues, the issue reference syntax has changed. Please add the word "Bugzilla" to issue references. For example, Fix Bugzilla Issue 12345 or Fix Bugzilla 12345.(Reminder: the edit needs to be done in the Git commit message, not the GitHub pull request.)

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#10846"

tedgin added 3 commits August 17, 2025 07:22
In prepration for handling the edge cases in beta, the logic that
computes beta when gamma would overflow was moved to a separate
function. This will allow the logic to be called from multiple places in
beta.
The implemntation of beta was extend to handle the special cases of
beta(x, y) where when using the main algorithm to compute beta, results
in gamma(x), gamma(y), or gamma(x+y) not being finite, causing beta to
appear to not be defined even when it is.
@tedgin
Copy link
Contributor Author

tedgin commented Aug 23, 2025

Is this able to be merged?

@thewilsonator
Copy link
Contributor

Yes, sorry, was preoccupied with travel and Dconf.

@thewilsonator thewilsonator merged commit c2f56bf into dlang:master Aug 24, 2025
10 checks passed
@tedgin tedgin deleted the 10826 branch August 31, 2025 17:20
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.

4 participants