Is there a pow-function for mpz_t and double

Richard B. Kreckel kreckel at
Tue Apr 6 13:53:38 CEST 2010

Hi Martin!

Martin Schmidt wrote:
>>> I have a large integer (mpz_t or mpz_class) x and a floating point (C++
>>> double) value y and want to compute x to the power of y (x^y).
>> You probably want to look at mpfr. What kind of result are you
>> expecting? An integer ? Is your double really an integer (or maybe 1/2) ?
> 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.

Since you're using the symbol Z and the term "integer", you should be 
aware that the result is not necessarily a real number. In GiNaC [0], 
this works automatically, thanks to CLN [1]. Here is an example:

rbk at wallace:~$ ginsh
ginsh - GiNaC Interactive Shell (ginac V1.5.7)
   __,  _______  Copyright (C) 1999-2010 Johannes Gutenberg University 
  (__) *       | Germany.  This is free software with ABSOLUTELY NO 
   ._) i N a C | You are welcome to redistribute it under certain 
<-------------' For details type `warranty;'.

Type ?? for a list of help topics.
 > evalf((-1)^Pi);

One can directly compute your result using plain CLN like this:

#include <iostream>
#include <cln/cln.h>
using namespace std;
using namespace cln;
int main()
     // integer base: 

     cl_I x("4294967296");
     // floating-point exponent (to as many digits as a double provides; 
more work, too, of course):
     cl_F y = cl_float(1.53169);
     // complex result: 

     cl_N r = expt(x,y);
     cout << r << endl;     // prints 5.6847154E14 



[0] <>
[1] <>
Richard B. Kreckel

More information about the gmp-discuss mailing list