Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong result for lower incomplete gamma function and x = 0 #1249

Open
cohomology opened this issue Apr 1, 2025 · 2 comments · May be fixed by #1251
Open

Wrong result for lower incomplete gamma function and x = 0 #1249

cohomology opened this issue Apr 1, 2025 · 2 comments · May be fixed by #1251

Comments

@cohomology
Copy link

cohomology commented Apr 1, 2025

Computation of tgamma_lower or any other incomplete gamma function yields wrong results for x = 0.

I am directly referring to special_functions/gamma.hpp, function gamma_incomplete_imp_final and for simplicity we compute with type T = double.

When trying to compute the lower incomplete gamma function with
a = 1 and x = 0, the check:

   if(a <= 0)
      return policies::raise_domain_error<T>(function, "Argument a to the incomplete gamma function must be greater than zero (got a=%1%).", a, pol);
   if(x < 0)
      return policies::raise_domain_error<T>(function, "Argument x to the incomplete gamma function must be >= 0 (got x=%1%).", x, pol);

lets everything through and the computation continues.

Later we have the following check:

    if(T(-0.4) / log(x) < a)
      {
         eval_method = 2;
      }
      else
      {
         eval_method = 3;
      }

This sets errno to EDOM and eval_method to 2, as log(0) is -Inf.

Now the computation continues and continues, but errno is never set back. Thus the caller, when he chose the ERRNO error propagation method, gets the wrong result overflow, where the result should be 0.

@cohomology cohomology changed the title Strange errno/error handling and wrong results for lower incomplete gamma function Wrong result for lower incomplete gamma function and x = 0 Apr 1, 2025
@cohomology
Copy link
Author

I am working on a fix

cohomology added a commit to cohomology/math that referenced this issue Apr 1, 2025
In this case, the errno error handling did not work correctly,
as internal functions where accidently setting it, although no
overflow happens.

Fixes boostorg#1249.
cohomology added a commit to cohomology/math that referenced this issue Apr 1, 2025
In this case, the errno error handling did not work correctly,
as internal functions where accidently setting it, although no
overflow happens.

Fixes boostorg#1249.
cohomology added a commit to cohomology/math that referenced this issue Apr 2, 2025
In this case, the errno error handling did not work correctly,
as internal functions where accidently setting it, although no
overflow happens.

Fixes boostorg#1249.
@cohomology cohomology linked a pull request Apr 2, 2025 that will close this issue
@cohomology
Copy link
Author

What I am wondering is that there are many instances of log(x) or 1 / x in gamma.hpp and generally no checks for x != 0 anywhere. Just happily applied. Perhaps worth to check them all at some point.

cohomology added a commit to cohomology/math that referenced this issue Apr 2, 2025
In this case, the errno error handling did not work correctly,
as internal functions where accidently setting it, although no
overflow happens.

Fixes boostorg#1249.
cohomology added a commit to cohomology/math that referenced this issue Apr 2, 2025
In this case, the errno error handling did not work correctly,
as internal functions where accidently setting it, although no
overflow happens.

Fixes boostorg#1249.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant