How can I get the best precision in an equation involving factorial, division and squaring?
As an example, I'm trying to make the binomical probability calculator in python:
# n: Number of trials # X: Number of successes # p: Probability of success on a single trial def binominal(n, X, p): return ( math.factorial(n) / (math.factorial(n - X) * math.factorial(X)) ) * ((p**X) * ((1-p)**(n-X))) print(binominal(5, 1, 0.166666666666)) Output:
0.4018775720164609 Now comparing with the binomial probability gained from an online calculator:
0.3468305983 My precision is way off. How can I get a better precision? I looked at the Python documentation and then tried to use the Decimal form instead, I didn't really get a better result from that.
0.40187757202from the online calculator.0.40187757202from the online calculator. It looks like your code runs correctly.