Search code examples
pythonfactorialfloating-point-conversion

Python: How to express a division of factorials in decimal format?


I'm trying to obtain the coefficients for Laguerre polynomials, this involves division of factorials, this is my code:

Coeficientes=[]
def Ln(n):
    for m in xrange(n+1):
        Coeficientes.append(((-1)**m *factorial(n))/(factorial(n-m)*factorial(m)*factorial(m)))
    print "Coeficiente de grado", m, ":", Coeficientes[m]

The problem is that when I must obtain the coefficient with value 0.5, i get 0.

Example

Ln(2)

Result:

Coeficiente de grado 0 : 1
Coeficiente de grado 1 : -2
Coeficiente de grado 2 : 0

But must say :

Coeficiente de grado 2 : 0.5

If i write Ln(2.) it returns me an error: Integer argument expected, got float


Solution

  • You got Coeficiente de grado 2 : 0 and not Coeficiente de grado 2 : 0.5 because factorial accept only integer, alternative way is to use math.gamma(x)

    gamma is an extension of the factorial function to real numbers. (if you are using python 2.7 or 3.2)

    see pydoc here