mpz_pow question

Décio Luiz Gazzoni Filho decio at decpp.net
Thu Oct 16 13:53:46 CEST 2008


On Oct 14, 2008, at 2:12 PM, Digital Parasite wrote:

> I'm new to GMP, so far my program is working great but I don't
> understand why a couple of GMP mpz_pow related functions are missing.
> mpz_pow_ui and mpz_ui_pow_ui only allow you to use an unsigned long
> int for the exponent.
>
> Is there a reason GMP doesn't include mpz_pow and mpz_ui_pow so that
> you can also use an mpz_t for the exponent?
>
> If I want to do something like 2^n or 3^p right now I need to use
> mpz_ui_pow_ui(rop, 2, mpz_get_ui(n)); or mpz_ui_pow_ui(rop, 3,
> mpz_get_ui(p)); for it to work and then it will only accept exponents
> in the unsigned long int range.
>
> Is there another way for me to do this and I just can't see or is
> mpz_pow() so long it wouldn't make any sense to use values larger than
> an unsigned long int?

Have you stopped to think about what you're asking for? You want  
exponents larger than, at a minimum, 2^32 - 1? (Make that 2^64 - 1 in  
a 64-bit system.) Do you realize this is, at best, if your base is 2  
and your exponent just barely overflows the range of a 32-bit  
integers, a 4 billion bit integer, which takes at least 512 megabytes  
of RAM to store?

If you're doing multi-precision arithmetic in what will be later used  
as an exponent of a non-modular powering, you're doing it wrong. Or  
maybe you really know what you're doing, but you wouldn't be asking  
that here, now would you? Redeclare those exponents as a primitive  
type and do the arithmetic there. You'll save time, code memory, data  
memory and get a more concise notation to boot.

GMP could add these functions, for the case of people who really know  
what they're doing, as convenience functions which save the programmer  
the chore of an extra mpz_get_ui() call, which would probably be done  
internally at the function anyway. But this would just invite trouble  
from the noob brigade flooding lists with question about why their  
code fails when trying to allocate more memory than there are  
elementary particles in the universe.

Décio


More information about the gmp-discuss mailing list