cmath.pyx 574 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
cdef extern from "math.h":
    double c_lgamma "lgamma" (double)
    double c_exp "exp" (double)

def exp(n):
    """Return e**n."""
    return c_exp(n)

def lfactorial(n):
    """Return an estimate of the log factorial of n."""
    return c_lgamma(n+1)

def factorial(n):
    """Return an estimate of the factorial of n."""
    return c_exp( c_lgamma(n+1) )


if __name__ == "__main__":
    import sys
    if len(sys.argv) != 2:
        sys.stderr.write("USAGE: %s n\nPrints n!.\n" % sys.argv[0])
        sys.exit(1)
    n = map(float, sys.argv[1:])
    print factorial(n)