Skip to content

std.mathspecial.beta does not handle all edge cases correctly. #10826

Closed

Description

@tedgin

The beta function B(x,y) has lots of edge cases when approximating real numbers with a floating point type. Assume x and y are real numbers, then B(x,y) can be defined as Γ(x)Γ(y)/Γ(x+y). The function Γ(z) isn't defined when z is a non-positive integer, but it is otherwise continuous, and 1/Γ(z) exists everywhere, being 0 when z is a non-positive integer. This means that whenever x or y is a non-positive integer, B(x,y) doesn't exist. Also, whenever x+y is a non-positive integer Γ(x+y) diverges, 1/Γ(x+y) is 0. In these case, as long as neither x nor y is a non-positive integer, if x+y is a non-positive integer, B(x,y) = 0. Approximating the set of real numbers using a IEEE 754 floating point type further complicates things because 0 isn't represented. Instead, +0.0 and -0.0 are represented where the first is a value infinitesimally larger than 0 and the second is a value infinitesimally smaller than 0. The edge cases are too many to enumerate here, but each of them need to be addressed in the implementation of std.mathspecial.beta. I've verified that many aren't.

Activity

  1. tedgin commented on Nov 3, 2025

    @tedgin
    ContributorAuthor

    It appears that the PR that I provided didn't fix all of the edge cases. When x, y, or both are very large but finite or very small but non-zero, beta(x, y) often fails to compute B(x,y). Here are the failed cases that I found.

    unittest 
    {
        import std.math: isNaN, nextUp, nextDown;
        import std.mathspecial: beta;
    
        const ɛ = nextUp(0.0L);
        const M = nextDown(real.infinity);
    
        /* These all fail. */
        // assert(!isNaN(beta(ɛ, ɛ)));
        // assert(!isNaN(beta(ɛ, M)));
        // assert(!isNaN(beta(1, M)));
        // assert(!isNaN(beta(M, M)));
    }
    

    I'll provide an addition fix for these cases.

  2. 0xEAB commented on Feb 1, 2026

    @0xEAB
    Member

    @tedgin Has this one been resolved?

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions