Power x^y both mpz_t

Paul Leyland paul at leyland.vispa.com
Tue Jun 29 18:02:17 CEST 2010

On Tue, 2010-06-29 at 10:41 +0200, Torbjorn Granlund wrote:
> Paul Leyland <paul at leyland.vispa.com> writes:
>   To be honest, I occasionally wish there were a such a function defined
>   --- and not because I'm able to compute the result for a large exponent.
>   It's purely because I like to have all my computational numeric
>   variables to have mpz type and reserve (various flavours of) ints for
>   stuff such as loop control.   Yes, I know that I can and do convert from
>   mpz to unsigned long but to me that seems a scrappy way of doing it.
>   Aesthetics are important, IMO.
> Indeed.  GMP prioritises performance, sometimes at the expense of
> aesthetics.
> Keeping values that are known to fit an 'unsignged long' in mpz, can
> impose significant overhead.  But if you really want to do that, I
> suggest that you use
>   mpz_pow_ui(res, base, mpz_get_ui(small_exp_stored_in_an_mpz_for_aesthetics))
> in this particular case.

Which is pretty much what I generally do, though I declare it as a macro
when I "need" it (really, when I don't care about my program slowing
down by a few ppm). I recognize that if small_exp... is too big then bad
things will happen.

Would the macro be an acceptable addition to the GMP API?  If not, could
it be added to the documentation?  After all, it's one of the few FAQs
that I have seen appear several times on this list.


More information about the gmp-discuss mailing list