Wrong answer when calculating binomial coefficients using modified formula
<p>I am trying to write a function to calculate the binomial coefficients using this formula: <a href="https://i.stack.imgur.com/wsjJb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wsjJb.png" alt="enter image description here"></a></p>

<p>The problem I am having is that I can not mange to get the correct answer. This is an example of two ways I have tried to write the function.</p>

<pre><code>def binomial(n, i):
    total = 0

    for j in range(1, (n-i+1)):
        n = float(n)
        i = float(i)
        j = float(j)        

        product = (i+j) / j

        if total == 0:
            total = product
        else:
            total = total * product

    print '%.f' %total
</code></pre>

<p>or like this using numpy</p>

<pre><code>import numpy as np

def binomial_np(n, i):
    array = np.zeros(n-i+1)

    for j in range(1, (n-i+1)):
        s = float(j)
        n = float(n)
        i = float(i)

        array[j] = (i+s)/s

    array = array[1 : ]
    array = np.prod(array)    

    print '%.f' %array
</code></pre>

<p>Both of the functions produces almost the correct result. After looking around a bit on the forum I did find some other examples that do produce the correct result, like this one from <a href="https://stackoverflow.com/questions/26560726/python-binomial-coefficient">Python Binomial Coefficient</a> </p>

<pre><code>import math
x = int(input("Enter a value for x: "))
y = int(input("Enter a value for y: "))
if y == x:
    print(1)
elif y == 1:         # see georg's comment
    print(x)
elif y &gt; x:          # will be executed only if y != 1 and y != x
    print(0)
else:                # will be executed only if y != 1 and y != x and x &lt;= y
    a = math.factorial(x)
    b = math.factorial(y)
    c = math.factorial(x-y)  # that appears to be useful to get the correct result
    div = a // (b * c)
    print(div) 
</code></pre>

<p>The real question I have from this is if there is something wrong with the way I have written the formulas, or if it just isnt possible to get the correct answer this way because of how float's and number of decimals work in Python. Hope someone can point me in the right direction on what I am doing wrong here.</p>