2
0
mirror of https://github.com/boostorg/math.git synced 2026-01-19 04:22:09 +00:00

Improve binomial_coefficient.hpp coverage.

This commit is contained in:
jzmaddock
2024-02-21 17:36:19 +00:00
parent 1f983ba4e0
commit b1144cc94a
3 changed files with 45 additions and 9 deletions

View File

@@ -25,10 +25,7 @@ T binomial_coefficient(unsigned n, unsigned k, const Policy& pol)
BOOST_MATH_STD_USING
static const char* function = "boost::math::binomial_coefficient<%1%>(unsigned, unsigned)";
if(k > n)
return policies::raise_domain_error<T>(
function,
"The binomial coefficient is undefined for k > n, but got k = %1%.",
static_cast<T>(k), pol);
return policies::raise_domain_error<T>(function, "The binomial coefficient is undefined for k > n, but got k = %1%.", static_cast<T>(k), pol);
T result;
if((k == 0) || (k == n))
return static_cast<T>(1);

View File

@@ -6,6 +6,7 @@
#include <pch.hpp>
#include "test_binomial_coeff.hpp"
//
// DESCRIPTION:
// ~~~~~~~~~~~~

View File

@@ -4,6 +4,7 @@
// 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_MATH_OVERFLOW_ERROR_POLICY ignore_error
#include <boost/math/concepts/real_concept.hpp>
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
@@ -43,7 +44,7 @@ void test_binomial(T, const char* type_name)
#if !(defined(ERROR_REPORTING_MODE) && !defined(BINOMIAL_FUNCTION_TO_TEST))
using namespace std;
typedef T (*func_t)(T, T);
typedef T(*func_t)(T, T);
#if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
func_t f = &binomial_wrapper<T>;
#else
@@ -53,8 +54,8 @@ void test_binomial(T, const char* type_name)
#include "binomial_data.ipp"
boost::math::tools::test_result<T> result = boost::math::tools::test_hetero<T>(
binomial_data,
bind_func<T>(f, 0, 1),
binomial_data,
bind_func<T>(f, 0, 1),
extract_result<T>(2));
std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
@@ -66,8 +67,8 @@ void test_binomial(T, const char* type_name)
#include "binomial_large_data.ipp"
result = boost::math::tools::test_hetero<T>(
binomial_large_data,
bind_func<T>(f, 0, 1),
binomial_large_data,
bind_func<T>(f, 0, 1),
extract_result<T>(2));
std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
@@ -76,5 +77,42 @@ void test_binomial(T, const char* type_name)
handle_test_result(result, binomial_large_data[result.worst()], result.worst(), type_name, "binomial_coefficient", "Binomials: large arguments");
std::cout << std::endl;
#endif
//
// Additional tests for full coverage:
//
BOOST_CHECK_THROW(boost::math::binomial_coefficient<T>(2, 3), std::domain_error);
T tolerance = boost::math::tools::epsilon<T>() * 200;
#if LDBL_MAX_10_EXP > 320
BOOST_IF_CONSTEXPR(std::numeric_limits<T>::max_exponent10 > 320)
{
BOOST_CHECK_CLOSE_FRACTION(boost::math::binomial_coefficient<T>(1072, 522), static_cast<T>(8.5549524921358966076960008392254468438388716743112653656397e320L), tolerance);
}
else BOOST_IF_CONSTEXPR(std::numeric_limits<T>::has_infinity)
{
BOOST_CHECK_EQUAL(boost::math::binomial_coefficient<T>(1072, 522), std::numeric_limits<T>::infinity());
}
#else
BOOST_IF_CONSTEXPR(std::numeric_limits<T>::has_infinity)
{
BOOST_CHECK_EQUAL(boost::math::binomial_coefficient<T>(1072, 522), std::numeric_limits<T>::infinity());
}
#endif
#if LDBL_MAX_10_EXP > 4946
BOOST_IF_CONSTEXPR(std::numeric_limits<T>::max_exponent10 > 4946)
{
BOOST_CHECK_CLOSE_FRACTION(boost::math::binomial_coefficient<T>(16441, 8151), static_cast<T>(5.928641856224322477306131563286843903129818155323061805272e4946L), tolerance);
}
else BOOST_IF_CONSTEXPR(std::numeric_limits<T>::has_infinity)
{
BOOST_CHECK_EQUAL(boost::math::binomial_coefficient<T>(16441, 8151), std::numeric_limits<T>::infinity());
}
#else
BOOST_IF_CONSTEXPR(std::numeric_limits<T>::has_infinity)
{
BOOST_CHECK_EQUAL(boost::math::binomial_coefficient<T>(16441, 8151), std::numeric_limits<T>::infinity());
}
#endif
}