On Thu, Sep 16, 2021 at 06:33:58PM -0000, Jonatan  wrote:

> Currently, math.factorial only supports integers and not floats, 
> whereas the "mathematical" version supports both integers and floats. 

That's questionable.

(Warning: maths geek post follows.)

Mathematicians typically only use the notation n! (factorial) for 
non-negative integer arguments. If the argument could be any real 
number, they typically use Γ(x) (gamma).

It is true that n! = Γ(n+1) so from a pragmatic point of view, we could 
have the factorial function accept any float. The HP-48 calculator does 
that. But the more modern TI-Nspire calculator doesn't.

However, the gamma function is not the only way to extrapolate factorial 
to all real numbers. There are at least two well-known others:

* Gauss' pi function Π(x) = Γ(x+1) so that Π(n) = n!

* and Hadamard's gamma function H(x)

But there are also others that are not as well known:

http://www.luschny.de/math/factorial/hadamard/HadamardsGammaFunctionMJ.html

As the above link suggests, Euler's gamma function may be the most 
famous extrapolation of factorial, but it is perhaps not the best.

Although not everyone agrees with that:

https://math.stackexchange.com/questions/1537/why-is-eulers-gamma-function-the-best-extension-of-the-factorial-function-to

By the way, it is unclear why Legendre introduced the notation Γ with 
the shift of 1 compared to factorial. That shift simplifies some 
formulae, but complicates many others. The Hungarian-Irish mathematician 
Cornelius Lanczos called it "void of any rationality".

(I love it when mathematicians say what they're really thinking.)

So from a *mathematical* perspective:

1. Having factorial accept any real number may be strange and confusing 
if calculating combinations and permutations, or other applications 
where non-integer values make no sense for factorial.

2. If you want to extrapolate factorial to non-integer values, you might 
not want to extrapolate it using Euler's Γ (gamma) function.

And from a *programming* perspective:

1. What advantage do we have in writing `factorial(2.5)` if you want to 
calculate `gamma(3.5)`?

2. We lose the type checking that protects us from some classes of error 
in applications where extrapolating to floats makes no sense.

Looking at your implementation:

> def better_factorial(n):
>     return n * math.gamma(n)

that is not going to work:

>>> 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.)

In summary, I don't think that there is much real advantage in having 
the factorial function take float arguments and call gamma. And we 
certainly don't need a *second* factorial function that is exactly the 
same as gamma. Just use gamma directly.


-- 
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JMLHEMN3VW54PNOFMU3CQLGR4VYDYEBR/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to