17.09.21 05:23, Steven D'Aprano пише:
factorial(23) == better_factorial(23) False
The problem is that since floats only have about 17 significant figures or so, once you get to 23! the gamma function doesn't have enough precision to give an exact answer. Of course that's easily fixable. (Only call gamma if the input argument is a float.)
It is not even easily fixable. First, the resulting function which uses different algorithms for integers and non-integers can be non-monotonic. Second, factorial() supports large arguments for which gamma() raises an overflow error. Third, it will have non-uniform computational complexity. The time of calculating gamma() is virtually constant, but the time of calculating factorial() grows with increasing argument.