title: Integer calculation of the binomial coefficient using boost::math::binomial_coefficient, returning value as boost::multiprecision::cpp_int? How?
body: <p>I would like to calculate the binomial coefficient as an integer for up to about <code>numberLeaves=100, K=10</code>.  I believe this should be possible to store in about a 128 bit integer.</p>
  
  <p>Therefore, I'd like to use <code>boost::multiprecision::cpp_int</code> to store the result, and use <code>boost::math::binomial_coefficient&lt;boost::multiprecision::cpp_int&gt;</code> to calculate it:</p>
  
  <pre><code>// Invalid because the template argument must be a floating-point type!
  boost::multiprecision::cpp_int number_branch_combinations = 
      boost::math::binomial_coefficient&lt;boost::multiprecision::cpp_int&gt;(numberLeaves, K);
  </code></pre>
  
  <p>Unfortunately, even though the binomial coefficient is an integer, the above code is invalid because <code>boost::math::binomial_coefficient</code> requires that the <strong>return value</strong> must be a floating point type, <a href="http://www.boost.org/doc/libs/1_55_0/libs/math/doc/html/math_toolkit/factorials/sf_binomial.html" rel="nofollow">claiming that</a>:</p>
  
  <blockquote>
    <p>...the template argument must be a real-valued type such as float or
    double and not an integer type - that would overflow far too easily!</p>
  </blockquote>
  
  <p>As noted, in my case I expect the result of the binomial coefficient calculation to fit within about 128 bits - and I'd like it as an integer.</p>
  
  <p>Therefore, I considered passing <code>boost::multiprecision::cpp_dec_float</code> as the template argument to <code>boost::math::binomial_coefficient</code>, and then doing a <strong>conversion</strong> from the floating-point return value (via rounding) to the nearest integer.</p>
  
  <p>Sadly, I can find no way to convert from a <code>boost::multiprecision::cpp_dec_float</code> to a <code>boost::multiprecision::cpp_int</code>.  It seems that performing a lossy conversion is <a href="http://www.boost.org/doc/libs/1_55_0/libs/multiprecision/doc/html/boost_multiprecision/tut/conversions.html" rel="nofollow">strictly prohibited</a> by the <code>boost::multiprecision</code> library:</p>
  
  <pre><code>cpp_int             cppi(2);
  cpp_dec_float_50    df(cppi);    // OK, int to float
  df                  = static_cast&lt;cpp_dec_float_50&gt;(cppr);  // OK, explicit rational to float conversion
  // However narrowing and/or implicit conversions always fail:
  cppi                =   df;    // Compiler error, conversion not allowed
  </code></pre>
  
  <p>Now I'm in the process of simply writing my own function to calculate the binomial coefficient, and return the value as an integer.</p>
  
  <p>I find it hard to believe that there is literally no possible way to use <code>boost::math::binomial_coefficient</code> to calculate the binomial coefficient and return it as a <code>boost::multiprecision::cpp_int</code>.</p>
  
  <p>Is it possible to use <code>boost::math::binomial_coefficient</code> to calculate the binomial coefficient and return it as a <code>boost::multiprecision::cpp_int</code>?</p>