Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 9 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_GPU_ENABLED T full_igamma_prefix(T a, T z, const Policy& pol)
{
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 @@ template <class T, class Policy, class Lanczos>
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 @@ BOOST_MATH_GPU_ENABLED T gamma_incomplete_imp_final(T a, T x, bool normalised, b

int eval_method;

if(is_int && (x > 0.6))
if (x == 0)
{
eval_method = 2;
}
else if(is_int && (x > 0.6))
{
// calculate Q via finite sum:
invert = !invert;
Expand Down Expand Up @@ -1627,7 +1631,7 @@ BOOST_MATH_GPU_ENABLED T gamma_incomplete_imp_final(T a, T x, bool normalised, b
#endif
result = gam - result;
}
if(p_derivative)
if(p_derivative && x > 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to let this line execute without the check for x > 0, that way our root finder will get a derivative back: in this particular case, any arbitrary large value will do.... ah but there should be an else before the *p_derivative /= x;.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I adjusted the coding. Is it okay now?

I also corrected some wrong ternary operators in the file which prevent using some functions with non-standard float/double T template parameters.

{
//
// Need to convert prefix term to derivative:
Expand Down Expand Up @@ -1660,7 +1664,7 @@ BOOST_MATH_GPU_ENABLED T gamma_incomplete_imp(T a, T x, bool normalised, bool in

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
145 changes: 145 additions & 0 deletions test/git_issue_1249.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// (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>
#include <boost/multiprecision/cpp_bin_float.hpp>

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

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, typename T>
void test_type_dispatch(T val)
{
if (val <= (std::numeric_limits<float>::max)())
test_impl<float, Fun>(static_cast<float>(val));
if (val <= (std::numeric_limits<double>::max)())
test_impl<double, Fun>(static_cast<double>(val));
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
test_impl<long double, Fun>(static_cast<long double>(val));
#endif
test_impl<cpp_bin_float_50, Fun>(static_cast<cpp_bin_float_50>(val));
}

template<template<typename> class Fun>
void test_impl()
{
test_type_dispatch<Fun, double>(1.0);
test_type_dispatch<Fun, double>(0.1);
test_type_dispatch<Fun, double>(0.5);
test_type_dispatch<Fun, double>(0.6);
test_type_dispatch<Fun, double>(1.3);
test_type_dispatch<Fun, double>(1.5);
test_type_dispatch<Fun, double>(2);
test_type_dispatch<Fun, double>(100);
test_type_dispatch<Fun, double>((std::numeric_limits<float>::max)());
test_type_dispatch<Fun, double>((std::numeric_limits<double>::max)());
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
test_type_dispatch<Fun, long double>((std::numeric_limits<long double>::max)());
#endif
}

void test_derivative()
{
double derivative = 0;
double result = boost::math::detail::gamma_incomplete_imp(1.0, 0.0,
true, false, c_policy(), &derivative);

Check warning on line 132 in test/git_issue_1249.cpp

View check run for this annotation

Codecov / codecov/patch

test/git_issue_1249.cpp#L132

Added line #L132 was not covered by tests
BOOST_CHECK(errno == 0);
BOOST_CHECK_EQUAL(derivative, 0);
BOOST_CHECK_EQUAL(result, 0);
}

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