Is there a pow-function for mpz_t and double

Paul Zimmermann Paul.Zimmermann at
Tue Apr 6 13:32:33 CEST 2010


> I expect an floating point value as the result. Maybe, an example makes
> it more clear: Given a large integer x = 4294967296 and a floating point
> value y = 1.53169, i want to compute x to the power of y, ie.
> 4294967296^1.53169
> Mathematically speaking the situation is that of a mapping
> f: Z x R -> R, (x,y) -> x^y
> with integer values Z and real numbers R. Before I am looking at the
> MPFR library I really would like to know, if there is really no
> possibility for doing this in the GNU MP library.

no, there is no (direct) possibility using GNU MP. However you can get an
approximation of the result by writing y ~ k/2^m with k, m integers, and
using the mpf_pow_ui(x,k) function and m times the mpf_sqrt function. But
while doing this, you might encounter intermediate overflows or underflows.

Here is how it would work with MPFR:

#include <gmp.h>
#include <mpfr.h>

main ()
  mpfr_t x, y, z;

  mpfr_init2 (x, 17); /* initialize x to 17 bits of precision */
  mpfr_init2 (y, 42); /* y has 42 bits of precision */
  mpfr_init2 (z, 100); /* we want 100 bits for z */
  mpfr_set_ui (x, 4294967296, GMP_RNDZ); /* round 4294967296 toward zero */ 
  mpfr_set_str (y, "1.53169", 10, GMP_RNDU); /* round 1.53169 to +Inf */
  mpfr_pow (z, x, y, GMP_RNDN); /* round z to nearest */
  mpfr_printf ("z=%Re\n", z); /* print z with rounding to nearest */
  mpfr_clear (x);
  mpfr_clear (y);
  mpfr_clear (z);
  return 0;

Hope this helps,
Paul Zimmermann

More information about the gmp-discuss mailing list