C++ factorial, primorial
Torbjörn Granlund
tg at gmplib.org
Tue Jun 24 14:38:02 UTC 2014
Marc Glisse <marc.glisse at inria.fr> writes:
I am trying to make a few more functions easily available from the C++
interface. mpz_fac_ui takes an unsigned long, so it would be natural
to have a factorial function that takes any of {signed,unsigned}
{char,short,int,long}. It would probably throw an out_of_range error
for negative values. But since it does not take a gmp type as
argument, I should not make it a free function in the global
namespace. The easiest seems to be a static member function, so we
would call:
mpz_class::factorial(42);
I don't understand what a "free function in the global namespace" might
mean, which makes it tough for me to make a good answer.
If we have an mpz_class that represents a small enough number, we
would probably like to be able to use factorial on it directly without
explicitly extracting an unsigned long. We could have
mpz_class::factorial(z); but since there is a GMP argument, this
versions could also be available as simply factorial(z);. Because of
the way it would be done with templates, factorial(42) would not
compile (it would not automatically convert 42 to mpz_class), which is
good. However, I think we could do without this global factorial for
now, and it can be added later if people want it.
I suppose I'd need to understand the alterantives, i.e., how user code
would look as a result of these design decisions.
Torbjörn
Please encrypt, key id 0xC8601622
More information about the gmp-discuss
mailing list