Skip to content

Fix lower incomplete gamma functions with x = 0 #1250

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions include/boost/math/special_functions/gamma.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@
{
BOOST_MATH_STD_USING

if (z > tools::max_value<T>())
if (z > tools::max_value<T>() || (a > 0 && z == 0))
return 0;

T alz = a * log(z);
Expand Down Expand Up @@ -962,7 +962,7 @@
BOOST_MATH_GPU_ENABLED T regularised_gamma_prefix(T a, T z, const Policy& pol, const Lanczos& l)
{
BOOST_MATH_STD_USING
if (z >= tools::max_value<T>())
if (z >= tools::max_value<T>() || (a > 0 && z == 0))
return 0;
T agh = a + static_cast<T>(Lanczos::g()) - T(0.5);
T prefix;
Expand Down Expand Up @@ -1297,7 +1297,11 @@

int eval_method;

if(is_int && (x > 0.6))
if (x == 0)
{
eval_method = 8; // do nothing
}
else if(is_int && (x > 0.6))
{
// calculate Q via finite sum:
invert = !invert;
Expand Down Expand Up @@ -1606,6 +1610,11 @@
result *= incomplete_tgamma_large_x(a, x, pol);
break;
}
case 8:
// x is zero, result is zero.
if (p_derivative)
*p_derivative = 0;

Check warning on line 1616 in include/boost/math/special_functions/gamma.hpp

View check run for this annotation

Codecov / codecov/patch

include/boost/math/special_functions/gamma.hpp#L1616

Added line #L1616 was not covered by tests
break;
}

if(normalised && (result > 1))
Expand All @@ -1627,7 +1636,7 @@
#endif
result = gam - result;
}
if(p_derivative)
if(p_derivative && x > 0)
{
//
// Need to convert prefix term to derivative:
Expand Down Expand Up @@ -1660,7 +1669,7 @@

T result = 0; // Just to avoid warning C4701: potentially uninitialized local variable 'result' used

if(a >= max_factorial<T>::value && !normalised)
if(x > 0 && a >= max_factorial<T>::value && !normalised)
{
//
// When we're computing the non-normalized incomplete gamma
Expand Down
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ test-suite special_fun :
[ run git_issue_1139.cpp ]
[ run git_issue_1175.cpp ]
[ run git_issue_1194.cpp ]
[ run git_issue_1249.cpp /boost/test//boost_unit_test_framework ]
[ run special_functions_test.cpp /boost/test//boost_unit_test_framework ]
[ run test_airy.cpp test_instances//test_instances pch_light /boost/test//boost_unit_test_framework ]
[ run test_bessel_j.cpp test_instances//test_instances pch_light /boost/test//boost_unit_test_framework ]
Expand Down
123 changes: 123 additions & 0 deletions test/git_issue_1249.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// (C) Copyright Kilian Kilger 2025.
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#define BOOST_TEST_MAIN

#include <boost/test/unit_test.hpp>
#include <boost/test/tools/floating_point_comparison.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/test/results_collector.hpp>
#include <boost/math/special_functions/gamma.hpp>

using namespace std;
using namespace boost::math;
using namespace boost::math::policies;

typedef policy<
policies::domain_error<errno_on_error>,
policies::pole_error<errno_on_error>,
policies::overflow_error<errno_on_error>,
policies::evaluation_error<errno_on_error>
> c_policy;

template<typename T>
struct test_lower
{
T operator()(T a, T x) const
{
return tgamma_lower(a, x, c_policy());
}

T expected(T a) const
{
return T(0.0);
}
};

template<typename T>
struct test_upper
{
T operator()(T a, T x) const
{
return tgamma(a, x, c_policy());
}
T expected(T a) const
{
return tgamma(a, c_policy());
}
};

template<typename T>
struct test_gamma_p
{
T operator()(T a, T x) const
{
return gamma_p(a, x, c_policy());
}
T expected(T) const
{
return T(0.0);
}
};

template<typename T>
struct test_gamma_q
{
T operator()(T a, T x) const
{
return gamma_q(a, x, c_policy());
}
T expected(T) const
{
return T(1.0);
}
};

template<typename T, template<typename> class Fun>
void test_impl(T a)
{
Fun<T> fn;
errno = 0;
T x = T(0.0);
T result = fn(a, x);
int saveErrno = errno;

errno = 0;

T expected = fn.expected(a);

BOOST_CHECK(errno == saveErrno);
BOOST_CHECK_EQUAL(result, expected);
}

template<template<typename> class Fun>
void test_type_dispatch(float a)
{
test_impl<float, Fun>(a);
test_impl<double, Fun>(double(a));
test_impl<long double, Fun>(static_cast<long double>(a));
}

template<template<typename> class Fun>
void test_impl()
{
test_type_dispatch<Fun>(1.0);
test_type_dispatch<Fun>(0.1);
test_type_dispatch<Fun>(0.5);
test_type_dispatch<Fun>(0.6);
test_type_dispatch<Fun>(1.3);
test_type_dispatch<Fun>(1.5);
test_type_dispatch<Fun>(2);
test_type_dispatch<Fun>(100);
test_type_dispatch<Fun>(std::numeric_limits<float>::max());
}

BOOST_AUTO_TEST_CASE( test_main )
{
test_impl<test_lower>();
test_impl<test_upper>();
test_impl<test_gamma_p>();
test_impl<test_gamma_q>();
}
Loading