summaryrefslogtreecommitdiff log msg author committer range
blob: 8fa2e26d8499de19bbbd39dac65b0bb2dd72283f (plain)
 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 ``` ``````// Copyright John Maddock 2006. // 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) #ifndef BOOST_MATH_SF_BINOMIAL_HPP #define BOOST_MATH_SF_BINOMIAL_HPP #ifdef _MSC_VER #pragma once #endif #include #include #include #include namespace boost{ namespace math{ template T binomial_coefficient(unsigned n, unsigned k, const Policy& pol) { BOOST_STATIC_ASSERT(!boost::is_integral::value); BOOST_MATH_STD_USING static const char* function = "boost::math::binomial_coefficient<%1%>(unsigned, unsigned)"; if(k > n) return policies::raise_domain_error( function, "The binomial coefficient is undefined for k > n, but got k = %1%.", k, pol); T result; if((k == 0) || (k == n)) return 1; if((k == 1) || (k == n-1)) return n; if(n <= max_factorial::value) { // Use fast table lookup: result = unchecked_factorial(n); result /= unchecked_factorial(n-k); result /= unchecked_factorial(k); } else { // Use the beta function: if(k < n - k) result = k * beta(static_cast(k), static_cast(n-k+1), pol); else result = (n - k) * beta(static_cast(k+1), static_cast(n-k), pol); if(result == 0) return policies::raise_overflow_error(function, 0, pol); result = 1 / result; } // convert to nearest integer: return ceil(result - 0.5f); } // // Type float can only store the first 35 factorials, in order to // increase the chance that we can use a table driven implementation // we'll promote to double: // template <> inline float binomial_coefficient >(unsigned n, unsigned k, const policies::policy<>& pol) { return policies::checked_narrowing_cast >(binomial_coefficient(n, k, pol), "boost::math::binomial_coefficient<%1%>(unsigned,unsigned)"); } template inline T binomial_coefficient(unsigned n, unsigned k) { return binomial_coefficient(n, k, policies::policy<>()); } } // namespace math } // namespace boost #endif // BOOST_MATH_SF_BINOMIAL_HPP ``````